views:

61

answers:

1

Basically what I need is an script that, when provided with a time and a timezone can return the time in another time zone.

My main issues are:

  • Where to get the time offset from GMT from - is there a public database available for this?
  • How to also take into consideration the daylight saving time (DST) differences as well.
  • How to nicely wrap it all up inside an PHP class - or is there such a class already available?

Constantin TOVISI

+3  A: 
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";

$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>

The above examples will output:

2000-01-01 00:00:00+12:00
2000-01-01 01:45:00+13:45

found on DateTime Manual on php.net

EDIT: Like Pekka said: The DateTime class exists from 5.2 on and there you first have to find out which of the methods are realy implemented and which one only exist from 5.3 on.

ITroubs
Beat me to the punch, deleting mine. It's worth mentioning that DateTime is available since PHP 5.2 - there is a patch for 5.1 but it's experimental
Pekka
Yep. datetime is pretty "new". Even in 5.2 it doesn't contain all the functions that are needed, wanted and mentioned in the manual
ITroubs
Good answer. These functions automatically handle daylight saving too, where simply adding an offset is clunky and inaccurate.
David Caunt