开发者

strtotime() different outputs for today's time

开发者 https://www.devze.com 2023-03-26 02:42 出处:网络
In PHP , i\'ve used the following code : echo strtotime(\"today\").\"<br>\"; echo strtotime(\"06/08/2011\").\"<br>\";

In PHP , i've used the following code :

echo strtotime("today")."<br>";
echo strtotime("06/08/2011")."<br>";
开发者_开发百科

I'm getting two different outputs :O

Why is that ?

Thanks


You get different outputs because the second one is not today, as you think. The second date is read actually as 08 June 2011.

If you write this code:

echo date("d M Y @ H:i", strtotime("today"))."<br/>";
echo date("d M Y @ H:i", strtotime("06/08/2011"))."<br/>";

You can see what I'm saying.

The second line should be:

strtotime("08/06/2011");


06/08/2011 is being interpreted as month-day-year, so June 8th, 2011.

From the docs:

Note:

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.


Because in some locales "6/8/2011" means "June 8", and in other locales it means "August 6".

For me (in the U.S.), it means "June 8".

Here's the PHP documentation which discusses setlocale() and LC_TIME:

http://php.net/manual/en/function.setlocale.php


strtotime("today"); is getting the exact time it is right now.

You can run time() to get the same result.

strtotime("06/08/2011") is getting the time at midnight (00:00) of the day you specified.
By the way, the day you specified is June 08, 2011. PHP doesn't handle dd/mm/yyy by default. You would have to specify that that's your locale default setting.


It also depends on PHP version:

  • in PHP 4.4.6: "today" is identical to "now", actual datetime
  • in PHP 5.1.4: "today" means the midnight of today

Source

0

精彩评论

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