Examples

Examples -- Learning by doing

Introduction

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.

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.

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.

Calculating a time difference

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.

Note: Date_Span works, unlike Date, internally with integers, which means that you have a precision of 32 bit or 68 years.

Converting timezones

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.

Sorting an array of dates

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.