Calculating GMT offsets
Another piece of information that's sometimes useful when working with time zones is the GMT offset -- that is, the difference between the specified time zone and standard GMT. The PEAR Date class lets you get this information easily, via its getRawOffset() method. Here's an example:
<?php
// include class
include ("Date.php");
// initialize object
$d = new Date("2006-06-21 10:36:27");
// set local time zone
$d->setTZByID("PST");
// get raw offset from GMT, in msec
echo $d->tz->getRawOffset();
?>
Here, the getRawOffset() method calculates the time difference between the local time and GMT. Here's the output:
-28800000
Note that this offset value is expressed in milliseconds, so you will need to divide it by 3600000 (the number of milliseconds in one hour) to calculate the time zone difference in hours.
Tip: You can use the inDaylightTime() method to see if the destination is currently observing daylight savings time. Look in the class documentation for details on this method.
Adding and subtracting timespans
The Date class also lets you perform sophisticated date arithmetic on temporal values, adding or subtracting durations to a date/time value. These durations (or timespans) are expressed as a string containing day, hour, minute and/or second components.
<?php
// include class
include ("Date.php");
// initialize object
$d = new Date("2006-06-21 15:45:27");
// add 01:20 to it
$d->addSpan(new Date_Span("0,1,20,0"));
// retrieve date as formatted string
echo $d->format("%A, %d %B %Y %T");
?>
In this case, I've added an hour and twenty minutes to the initial timestamp, by calling the Date class' addSpan() method and supplying it with a Date_Span() object initialised to that duration. The output is fairly easy to guess:
Wednesday, 21 June 2006 17:05:27
Just as you can add timespans, so too can you subtract them. That, in fact, is the purpose of the subtractSpan() method, which is illustrated below.
<?php
// include class
include ("Date.php");
// initialize object
$d = new Date("2006-06-21 15:45:27");
// add 01:20 to it
$d->addSpan(new Date_Span("0,1,20,0"));
// subtract 00:05 from it
$d->subtractSpan(new Date_Span("0,0,5,0"));
// retrieve date as formatted string
echo $d->format("%A, %d %B %Y %T");
?>
Here, I've first added an hour and twenty minutes, and then subtracted a further five minutes. The net effect is an addition of an hour and fifteen minutes, and the output reflects this:
Wednesday, 21 June 2006 17:00:27
As the examples above illustrate, the PEAR Date class provides methods to intuitively and efficiently perform fairly complex date math. If you're looking for a stress-free way to convert timestamps between different locations, I'd heartily recommend it to you. Take a look, and happy coding!
Do you need help with PHP? 



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:
» 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.
» 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