Jesdisciple
07-10-2010, 10:25 AM
I'm trying to compare the current time with one from another (SVN) server, set in GMT and formatted as 'D, d M Y H:i:s e', but I'm having problems getting a GMT/UTC timestamp from the supplied date-string. Unfortunately my deployment server is running PHP 5.1.6, so I can't take advantage of DateTime::getTimestamp (http://www.php.net/manual/en/datetime.gettimestamp.php) which would work perfectly for this. So I've pieced together a solution which I think should be portable across PHP 5.1+ installations but would appreciate feedback from anyone interested in giving it. (I started typing this as a question, but I'm not totally confident about the answer I formulated.)
Here's the code, where $dateString is the date retrieved from the other server.
$dateTime = new DateTime($dateString);
$timezone = new DateTimeZone(date_default_timezone_get());
$offset = timezone_offset_get($timezone, $dateTime);
$timestamp = strtotime($dateString) - $offset;
echo $dateString . '<br />' . PHP_EOL;/* These should be identical, except the timezone which my PHP insists on showing as "System/Localtime" below. */
echo date('D, d M Y H:i:s e', $timestamp) . '<br />' . PHP_EOL;
echo $timestamp . '<br />' . PHP_EOL;
Thanks for any input.
Here's the code, where $dateString is the date retrieved from the other server.
$dateTime = new DateTime($dateString);
$timezone = new DateTimeZone(date_default_timezone_get());
$offset = timezone_offset_get($timezone, $dateTime);
$timestamp = strtotime($dateString) - $offset;
echo $dateString . '<br />' . PHP_EOL;/* These should be identical, except the timezone which my PHP insists on showing as "System/Localtime" below. */
echo date('D, d M Y H:i:s e', $timestamp) . '<br />' . PHP_EOL;
echo $timestamp . '<br />' . PHP_EOL;
Thanks for any input.