PHP: Using DateTime Class to take the pain out of working with dates

28 May

Well, since few days I’m writing about object oriented programming. Object orientation has many advantages over structural programming. It not only lessen your burden by allow you to achieve your desired results with less code, but also enable you to reuse your code in as much places as you wish in your application.

Implementation Object orientation give lot of strength to PHP. Many people may still stick to conventional programming, but all will one day stop writing code in old style structural programming and adopt object oriented programming instead.

I am not going to discuss the benefits of object oriented programming and down sides of conventional, but instead share some knowledge about DateTime class available in the latest version of php.

If you have some knowledge of php and have developed any application, you’ll certainly come across a requirement demanding usage of date or time.

In conventional programming displaying date need to write the following.

echo ‘Current date is ‘ date(‘m-d-y’);

It displays the following.

Current date is 05-28-09

While using the DateTime class, you will need to write the following.

$date = new DateTime();

echo $date->format(‘l, F jS, Y’);

This gives the following result.

Thursday, May 28th, 2009

Well, you may feel that the conventional method is more easy to use, as it only require one line of code.

The Usage of the DateTime class comes handy when you wish to perform some complex tasks.

For example, if you like to display the next Thursday, you will need to writ the following code.

$date = new DateTime(‘next Thursday’);

echo $date->format(‘l, F jS, Y’);

The result of the about would be.

Thursday, June 4th, 2009

As today is Thursday, May 28th, 2009

Only passing “next Thursday” to the constructor, it display the next Thursday for us.

This is one simple usage of the DateTime class. Lets look at some other useful methods of this class.

To have the current date, write

$date = new DateTime();

If you want to set specified date, you can do it either by passing string argument to its constructor as

$date = new DateTime(“18-July-2008 16:30:30”);

And then display as

echo $date->format(‘l, F jS, Y’);

Or call empty constructor and then use setDate method as

$date = new DateTime();

$date->set(“year”,”month”,”day”);

For example

$date->setDate(“2008″,”05″,”18”);

Similarly you can set time as

$date->setTime(int $hour, int $minutes [, int $seconds]);

For example

$date->setTime(“16”,”30”,”30”);

Another useful method is modify for modification of the date time.

If you want to add a day to the current date, write

$date = new DateTime();

$date->modify(“+1 day”);

This add a single day to the current date and display the next day.

Similarly if you want to subtract a day, write

$date->modify(“-1 day”);

Also you can add and subtract month and years as

$date->modify(“+1 month”);

$date->modify(“+1 year”);

And

$date->modify(”-1 month”);

$date->modify(“-1 year”);

If you want to get the time zone, writ an single line

$date->getTimeZone()->getName();

And similarly setting time zone can be done like this.

$date->setTimeZone(new DateTimeZone(“Asia/Karachi”));

One Response to “PHP: Using DateTime Class to take the pain out of working with dates”

  1. SARFRAZ AHMED September 3, 2009 at 8:02 am #

    good info🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: