开发者

strtotime php function not behaving as expected

I'm trying to write a bit of php to determine the date (Y-m-d format) for the next occurrence of a given weekday.

date('Y-m-d', strtotime('next monday'));

is returning

2011-08-29

instead of

2011-08-22

on my server. Not sure why. My server is an EC2 instance in Amazon's US Ea开发者_开发技巧st-Coast data center and it's currently 2011-08-21 9:40:00.

EDIT 1 Also date('Y-m-d', strtotime('next sunday')); returns 2011-08-28 instead of today. I tried out this code on http://writecodeonline.com/php/ ... beginning to think that tester is inaccurate.


Just because your server is set to a certain timezone doesn't necessarily mean PHP will be acting on the same timezone. It is already Monday, August 22, 2011 in Europe, which also includes UTC. If your PHP is acting on this timezone, "next Monday" would be Aug. 29.

You should double-check your timezone settings in PHP. You can either run date_default_timezone_get() to check what settings your installation is using, or try forcing your PHP script to use the timezone you expect with date_default_timezone_set("America/New_York"); and seeing if you get the result you expect.

I just tested this:

date_default_timezone_set("America/New York");
echo date('Y-m-d', strtotime('next monday')); // prints 2011-08-22
date_default_timezone_set("UTC");
echo date('Y-m-d', strtotime('next monday')); // prints 2011-08-29


I think that's the expected behaviour, since Monday hasn't passed strtotime sees it as the Next Monday.


Which version of PHP are you using? In some versions of PHP strtotime does have a buggy behavior.

I found that in strtotime manual page:

5.0.2 In PHP 5 up to 5.0.2, "now" and other relative times are wrongly computed from today's midnight. This differs from other versions where it is correctly computed from current time.

5.0.0 Microseconds began to be allowed, but they are ignored.

4.4.0 In PHP versions prior to 4.4.0, "next" is incorrectly computed as +2. A typical solution to this is to use "+1".

Your described behavior suggests that you have a version between 5.0 and 5.0.2.

I suggest you to check whether the difference between today and the computed "next monday" is higher than 7 days. If it is, subtract 7 days and you have the actual date.

Try using that code:

$days = ((strtotime('next monday') - time()) / 60 / 60 / 24);
if ($days >= 8) {
  $days -= 7;
}

I know there is date_diff function, but if your PHP version is so low as I think, it isn't available for you (sorry, only 5.3.0 and higher).


"Next", when used with weekdays, generally means the first one after the next closest one. For example if today is Sunday the 21st then the "next" Monday is not tomorrow the 22nd but the following Monday the 29th.

I know this is not necessarily intuitive and not recognized universally but I believe that that is how strtotime() interprets "next".

EDIT: I have confirmed via my own tests that I am incorrect and that "next" should return the first upcoming weekday (the 22nd in my example above). I will leave this answer here unedited as it and the comment below may be helpful to others.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜