PEAR Date is a generic class to represent and work with dates. It does not use timestamps which means it can be used for dates before 1970 and after 2038. The class provides methods to convert a date in different formats, calculate differences, weekdays and more.
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.
Példa 36-2. 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.
Példa 36-3. 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.
Példa 36-4. Calculating a time span
|
Megjegyzés: 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 $list = Date_TimeZone::getAvailableIDs();.
Példa 36-5. 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.
Példa 36-6. Sorting dates
|