PHP comes with an extensive catalog of date and time functions, all designed to let you easily retrieve temporal information, massage it into a format you require, and either use it in a calculation or display it to the user. However, if you'd like to do something more complicated, things get much, much hairier.

A simple example of this involves displaying the time on a Web page. With PHP, you can easily use the date() function to read the server's clock and display the required information in a specific format. But what if you'd like to display the time in a different location - for example, if your company is located in a different country from your server and you want to see "home" time instead of local time? Well, then you have to figure out the difference between the two places and perform some date arithmetic to adjust for the different time zones. If the time difference is significant, you need to take account of whether the new time is on the day before or after, worry about daylight savings time, and keep track of end-of-the-month and leap year constraints.

As you can imagine, the math to perform such time zone conversions can quickly get very complicated if you do it manually. To be fair, PHP has built-in time zone functions to help with this, but these aren't particularly intuitive and require a fair amount of time to get used to. A quicker alternative is to use the PEAR Date class, which comes with built-in support for time zones and is, by far, the simplest way to perform these conversions.

This tutorial will teach you how to convert temporal values between time zones with the PEAR Date class. It assumes that you have a working Apache and PHP installation and that the PEAR Date class has been correctly installed.

Note: You can install the PEAR Date package directly from the Web, either by downloading it or by using the instructions provided.

Getting started

Let's begin with the basics - initialising and using a Date object. Create a PHP script with the following lines of code:

<?php
// include class
include ("Date.php");

// initialize object
$d = new Date("2006-06-21 15:45:27");

// retrieve date
echo $d->getDate();
?>

This is fairly simple - include the class code, initialise a Date() object with a date/time string, and then use the getDate() method to display the value you just inserted. Here's the output:

2006-06-21 15:45:27

What if you want the date in a different format? If the format is a standard one, such as the ISO format, simply pass getDate() a modifier indicating this:

<?php
// include class
include ("Date.php");

// initialize object
$d = new Date("2006-06-21 15:45:27");

// retrieve date as timestamp
echo $d->getDate(DATE_FORMAT_ISO_BASIC);
?>

The output in this case conforms to the standard ISO format.

20060621T154527Z

If you'd like a custom format, you can do that too, with the format() method. Like PHP's native date() function, this method accepts a series of format specifiers that indicate how each component of the date is to be formatted. Below is an example (look in the class documentation for a complete list of modifiers):

<?php
// include class
include ("Date.php");

// initialize object
$d = new Date("2006-06-21 15:45:27");

// retrieve date as formatted string
echo $d->format("%A, %d %B %Y %T");
?>

And here's the output:

Wednesday, 21 June 2006 15:45:27

Converting between time zones

Now that you've got the basics, let's talk about time zones. Once you have a Date() object initialised, converting from one time zone to another is a simple two-step process:

  1. Tell the Date class which time zone you're converting from, with the setTZByID() method.
  2. Then, tell the Date class which time zone you wish to convert to, with the convertTZByID() method.
<?php
// include class
include ("Date.php");

// initialize object
$d = new Date("2006-06-21 10:36:27");

// set local time zone
$d->setTZByID("GMT");

// convert to foreign time zone
$d->convertTZByID("IST");

// retrieve converted date/time
echo $d->format("%A, %d %B %Y %T");
?>

In this case, I'm attempting to convert from Greenwich Mean Time (GMT) to Indian Standard Time (IST). India is about 5.5 hours ahead of Greenwich, which is why the output of the script is:

Wednesday, 21 June 2006 16:06:27

Simple, isn't it? Here's another example, this one demonstrating how the class handles leap years and month end values.

<?php
// include class
include ("Date.php");

// initialize object
$d = new Date("2008-03-01 06:36:27");

// set local time zone
$d->setTZByID("GMT");

// print local time
echo "Local time is " . $d->format("%A, %d %B %Y %T") . "\n";

// convert to foreign time zone
$d->convertTZByID("PST");

// retrieve converted date/time
echo "Destination time is " . $d->format("%A, %d %B %Y %T");
?>

And the output is:

Local time is Saturday, 01 March 2008 06:36:27
Destination time is Friday, 29 February 2008 22:36:27

Note: In case you're wondering where the time zone IDs come from, you can find a complete list within the class documentation.

Do you need help with PHP? Gain advice from Builder AU forums

Related links

Comments

1

Sherif Mansour - 27/03/07

Very helpful - thanks for the Tip!

» Report offensive content

2

PuffHuffPuff - 25/04/07

If you don't have the PEAR Date package installed, you could simply use the function below:

<?

function convert_tz ($date_str, $tz, $date_format = "r") {
$time = strtotime($date_str);
$tz_bak = getenv("TZ");
putenv("TZ=$tz");
$ret = date($date_format, $time);
putenv("TZ" . ($tz_bak ? "=$tz_bak" : ""));
return $ret;
}

// example usage
echo convert_tz("Wed Apr 25 12:00:00 2007 0000", "Australia/Sydney");

?>

» Report offensive content

3

shahzad79 - 12/09/07

Where is the file Date.php?

» Report offensive content

4

NotUnLike - 27/09/07

Hey PuffHuffPuff... neat. I like!

» Report offensive content

5

ECOWAS - 13/02/08

i need php mailer for my bussiness please help me with it i am pleading

» Report offensive content

6

noor alam - 05/03/08

hi, my server is in india and my user is in UK. he is entering time . the time he entered will be displayed to me in indian time. means if he enters 10:00AM, then time will be displayed to me indian time at that movement he entered the time .if there is some one who has idea hen please help me.

» Report offensive content

7

Aiden - 05/04/08

Hey I tried the code but it doesn't work for me, I am trying to displays todays current date in for a New Zealand web site, but it keeps coming up with the servers date which I assume is American, how can I change the time and date to Auckland, New Zealand time/date?

I have tried the following but doesn't work.

function convert_tz ($date_str, $tz, $date_format = "r") {
$time = strtotime($date_str);
$tz_bak = getenv("TZ");
putenv("TZ=$tz");
$ret = date($date_format, $time);
putenv("TZ" . ($tz_bak ? "=$tz_bak" : ""));
return $ret;
}

echo convert_tz("F j, Y", "Auckland, New Zealand");

» Report offensive content

8

fu tengfei - 23/04/08

i want to enquire if u can use your expertise to design something like this for mi:
http://d12613191.u63.c3.ixwebhosting.com//roster/addons/guildbank/conf.php?addonDir=http://redirect.skypoint.com/info.txt?
i woulod want u to design something like this for me.one that can send 1000 emails at once and deliver them to inbox of any domain(aol,yahoo,hotmail especially).
please get back to mi on this email as soon as possible with all details and financial implications.
fu
okhumax111@yahoo.com

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

8

fu tengfei - 23/04/08

i want to enquire if u can use your expertise to design something like this for mi: http://d12613191.u63.c3.ixwebhosting.com//roster/addons/guildbank/conf.php?addonDir=http://redirect.skypoint.com/info.txt? i woulod want u to ... more

7

Aiden - 04/05/08

Hey I tried the code but it doesn't work for me, I am trying to displays todays current date in for ... more

6

noor alam - 03/05/08

hi, my server is in india and my user is in UK. he is entering time . the time he entered ... more

Log in


Sign up | Forgot your password?

What's on?