1
votes

Seems I don't quite understand much the function strtotime. My case is I would like to compare the current time (now) with a specific time on specific timezone

For example the specific time is "this Monday at 14:00:00" at the timezone "America/New_York":

 $specificTime  = strtotime("monday this week 14:00:00 America/New_York");

My current code is:

 $now  = strtotime("now America/New_York");
 if ($now > $specificTime) {
     //do something
 }

But I have figured it out that $now above is 6 hours ahead with current time. The number 6 I guess from offset -05:00 of America/New_York, plus with 1 hour daylight saving.

it should remove timezone out of $now, it will work correctly:

 $now  = strtotime("now");
 if ($now > $specificTime) {
     //do something
 }

Could someone give the explain why strtotime("now America/New_York") is 6 hours ahead with strtotime("now), why they are not equivalent? really confused.

P.S: I am on GMT+07:00.

3
You can always convert to different timezones if you know what they are by adding X seconds. - Jared
Why would you expect now and now America/New_York to be equivalent when your machine is in a completely different time zone? Also, strtotime is not magic. I'd never want to rely on it getting "monday this week 14:00:00 America/New_York" correct. Have you tried using DateTime, explicitly setting timezones and doing a little bit of date math? - deceze
Well, because strtotime return a timestamp and the timestamp does not depend on the timezone. So simple thinking is "now" in here is also equivalent with "now" in any place in the world. My point is why the difference is 6 hours ahead? - cuongle
Because if you are standing in New York now means the current time. Simultaneously if you are sleeping in Europe now is a different time (but the same instant). - Jim

3 Answers

4
votes

Simple debugging:

<?php

$now  = strtotime("now America/New_York");
echo date('r', $now);
// Thu, 28 Nov 2013 16:39:51 +0100

... shows that such command is doing this:

  1. Calculate local time in my default time zone (10:39:51 +0100)
  2. Return timestamp that corresponds to 10:39:51 in New York time (-0500)

Doing date manipulation with strings is terribly complicated. Just imagine you'd try to do math with string functions: strtofloat('one plus square root of half hundred')—there'd be plenty of room for mistakes. So my advise is to keep it simple and only use with simple expressions when there's some benefit, such as strtotime('+1 day').

If you need to work with different time zones, I suggest you use proper DateTime objects. If you choose to work with Unix timestamps, forget about time zones: Unix timestamps do not have time zone information at all.

0
votes

You can use DateTime for this. I believe settings a timezone in strtotime is not valid.

$specificTime = new DateTime("monday this week 14:00:00", new DateTimeZone("America/New_York")));

$now = new DateTime("now", new DateTimeZone("America/New_York"));

You can then compare unix timestamp with this:

if ($now->getTimestamp() > $specificTime->getTimestamp()) {
    // do something ...
}
0
votes

There is time offset between each timezone.

strtotime() function will return the Unix timestamp according the timezone.

It will use the default time zone unless a time zone is specified in that parameter.

The default time zone it the return value of date_default_timezone_get();

Look the code below:

<?php
// UTC
echo date_default_timezone_get(), "\n";

// 2013-11-28 14:41:37
echo date('Y-m-d H:i:s', strtotime("now America/New_York")), "\n";

// 2013-11-28 09:41:37
echo date('Y-m-d H:i:s', strtotime("now")), "\n";