Javascript validator
Web page editor
Following the saying "learning by doing", we start with some examples.
The first step to work with the class is to instantiate a Date object.
Example 40-1. Creating a Date object
|
After finishing your work with the date object, you probably want to have it back. This can be done in different ways, for example by using getTime or getDate.
Example 40-2. Getting the date in different formats
|
Date has a lot more methods to output your date, but that is subject of a later chapter.
Now that we know how to create a Date instance, we'll do some easy tasks.
You often have the task to know which time span lies between two dates. With Date, this is easy to accomplish. First we use setFromDateDiff on a fresh Date_Span object and then toDays to get the exact number of days between the two dates.
Example 40-3. Calculating a time span
|
Note: Date_Span works, unlike Date, internally with integers, which means that you have a precision of 32 bit or 68 years.
Date can help you working with time zones. An array with all supported timezones can be retrieved by using getAvailableIDs statically as in $list = Date_TimeZone::getAvailableIDs();.
convertTZ converts the Date object's internal settings to the given time zone. Using format you can display the timezone setting. With Date_TimeZone's getDefault method the default time zone for this computer can be obtained.
Example 40-4. Converting timezones
|
Once you have an array of Date objects, you might want to sort it. The class provides a static method compare that helps with this.
Example 40-5. Sorting dates
|
Javascript validator
Web page editor