1

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.

cuongle
  • 74,024
  • 28
  • 151
  • 206
  • You can always convert to different timezones if you know what they are by adding X seconds. – Jared Nov 28 '13 at 09:29
  • possible duplicate of [Why does strtotime give different result in different timezone?](http://stackoverflow.com/questions/7295167/why-does-strtotime-give-different-result-in-different-timezone) – Jared Nov 28 '13 at 09:30
  • 1
    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 Nov 28 '13 at 09:32
  • 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 Nov 28 '13 at 09:38
  • 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 Nov 28 '13 at 09:41
  • @Jim Actually, no, he's right. My bad as well. "Now" should be equal all across the world. But since it's obviously different, I'd blame it on `strtotime` interpreting the input differently than you want it to. Again: I simply wouldn't rely on its automagic here (which it doesn't have). – deceze Nov 28 '13 at 09:42
  • @deceze: Yeah, I guess the same reason as yours, `strtotime` interprets totally wrong in this case. – cuongle Nov 28 '13 at 09:48

3 Answers3

4

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.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 1
    "`+1 month`" is a bad example IMO. How often have we answered "+1 month doesn't work as I think it should" questions here... ;-) But I agree with the overall sentiment. – deceze Nov 28 '13 at 09:49
  • I've changed the example, just in case. I hope there aren't endless questions about `+1 day`. – Álvaro González Nov 28 '13 at 10:41
  • But I expected "21:30:59"! ;-) The problem is with the concept of "expectations" here... I'm just being fastidious. – deceze Nov 28 '13 at 10:52
0

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 ...
}
Marwelln
  • 28,492
  • 21
  • 93
  • 117
0

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";
srain
  • 8,944
  • 6
  • 30
  • 42