Log in

View Full Version : Adjusting timezones



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.

jscheuer1
07-10-2010, 10:34 AM
Maybe:

http://www.php.net/manual/en/reserved.variables.server.php

Scroll down that page to:


'REQUEST_TIME'
The timestamp of the start of the request. Available since PHP 5.1.0.

Jesdisciple
07-10-2010, 10:41 AM
Err, I guess I should have stated my problem at the first of that first sentence:
I'm having problems getting a GMT/UTC timestamp from the supplied date-string.

(For that matter, now that I have re-read my post I notice that the current time is not relevant to the issue at all. The time to be compared with that from the server is when a local file was last modified, but neither is that directly relevant to this question.)

bluewalrus
07-10-2010, 03:36 PM
So maybe http://www.php.net/manual/en/function.filemtime.php

Jesdisciple
07-10-2010, 04:16 PM
*facepalm* Sorry, no offense - I just can't believe you both got sidetracked by the "local filesystem" portion of this feature. I apparently explained my problem very poorly so I'll try again, with more background info this time. I left it out because normally I consider it fluff but I think it clears some things up in this case.

I'm developing the Kuva (http://code.google.com/p/kuva) project and recently adopted its inactive dependency phpsvnclient (http://code.google.com/p/phpsvnclient/). I'm trying to make Kuva able to update all of a site's components via SVN, hence the dependency. phpsvnclient supplies the paths (http://code.google.com/p/phpsvnclient/wiki/getDirectoryFiles) of its source tree along with a last-modified date. I want to convert that date into a timestamp on my server; I cannot access the files on the Google Code server to manually find this timestamp. The code I supplied above is my (hypothetical) solution for converting to the timestamp from which I can subtract the local last-modified (returned from filemtime).

Jesdisciple
07-11-2010, 01:36 PM
(bumpety) Is there really no cleaner way to convert a date-string to a timestamp?