Reputation: 665
Ran into an issue with strtotime() and time() this morning as the time changed back 1 hour.
date_default_timezone_set('America/New_York');
$expire = strtotime('+15 minutes');
if ($expire < time()) {
// session expired
}
Between the second 1-2am period, strtotime was producing times that were less than time(), so sessions were expired on creation. Why was that?
Upvotes: 1
Views: 155
Reputation: 5358
time()
returns the current Unix timestamp, which, by definition is seconds since 1st January 1970, UTC. UTC doesn't observe time zones.
You're setting the time zone to America/New_York
, then using strtotime()
to get the current local time, which does observe time zones, hence the discrepancy.
UTC was specifically introduced to provide a consistent and continuous time reference. If you need to perform operations that rely on a consistent time reference, always use UTC.
FWIW, whenever I have to deal with time issues I set UTC everywhere (PHP, database, etc) and convert to local time in the presentation code - usually done by JavaScript on the client.
Upvotes: 2