php strtotime() 在窗口 mac linux 中产生不同的结果?

发布于 2024-12-04 10:50:42 字数 361 浏览 1 评论 0原文

$today   = date("Y-m-d H:i");
$endtime = date("2011-09-14 00:00");
$second  = abs(time() - strtotime($endtime, strtotime($today, 0)));

echo $second;

我想找到未来日期 - 今天日期的第二个,但结果在所有平台上都不同,其中:

  • mac:21343,
  • ubuntu:68172和
  • window:50175。

将秒转换为小时:分钟:秒后,不同之处在于10-15小时。有人知道其他与 strtotime 类似的方法吗?

$today   = date("Y-m-d H:i");
$endtime = date("2011-09-14 00:00");
$second  = abs(time() - strtotime($endtime, strtotime($today, 0)));

echo $second;

I want to find the second from future date - today date, but the result is different in all platform where:

  • mac: 21343,
  • ubuntu: 68172 and
  • window: 50175.

After convert the second to hour:minute:second, the different is around 10-15 hours. Anyone know other method that work similar to strtotime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

叫思念不要吵 2024-12-11 10:50:42

可能是因为系统有不同的时区或系统时钟

Maybe because the systems have different timezones or system clocks

美羊羊 2024-12-11 10:50:42

你把事情搞得相当复杂了。 date("2011-09-14 00:00") 是无意义的,只是 "2011-09-14 00:00" 会做同样的事情。 strtotime($today, 0) 看起来也很奇怪。只有这样就可以了:

$second = abs(time() - strtotime('2011-09-14 00:00:00'));

确保使用 date_default_timezone_set 在所有系统上将时区设置为相同,这可能是您实际问题的根源。

You're making it quite complicated. date("2011-09-14 00:00") is nonsense, just "2011-09-14 00:00" will do the same thing. strtotime($today, 0) seems weird too. Only this should do just fine:

$second = abs(time() - strtotime('2011-09-14 00:00:00'));

Make sure you set the timezones to be the same on all systems using date_default_timezone_set, which is probably were your actual problem comes from.

后知后觉 2024-12-11 10:50:42

它可能从您的服务器(具有可变时间)获取时间设置,您可以在所有脚本上设置时区,以便为您提供相同的值。

date_default_timezone_set('Europe/London');; //I usually put it at the top of the script

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

It's probably getting the time settings from your server (which has variable time), you can set a timezone on all scripts for it to give you the same values.

date_default_timezone_set('Europe/London');; //I usually put it at the top of the script

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

不喜欢何必死缠烂打 2024-12-11 10:50:42

你的代码过于复杂并且有一些奇怪的地方,但这已经在 @deceze 的答案中指出了,所以我不会再讨论这个。

其他人也提出了您所看到的实际差异的可能原因,所以我也不会详细讨论。

我要说的是,我总是会尽可能避免使用 strtotime() 函数,因为它非常慢,并且当日期格式不明确时(即, dd-mm-yyyymm/dd/yyyy)。即使在像您这样的输入明确的情况下,它仍然是一段非常慢的代码,因为它必须在不知道期望的格式的情况下解析日期字符串。

如果您正在进行日期算术,最新版本的 PHP 提供了更好的选择。查看 date_diff() 函数,以及 DateTime 类 中的其他函数。与您正在使用的旧式 PHP 技术相比,此类提供了更好的日期处理功能。

请注意,为了充分利用 DateTime 类,您需要运行最新版本的 PHP - 即 PHP 5.3。但由于不再支持 PHP 5.2,您确实应该确保现在使用的是 5.3。

希望有帮助。

Your code is over complicated and has some weirdness in it, but this has already been pointed out in @deceze's answer, so I won't go over that again.

Others have also suggested possible reasons for the actual discrepancies you're seeing, so I won't go into that either.

What I will say is that I would always avoid using the strtotime() function where possible, because it is very slow and has the some potential for thowing unexpected results when the date is in an ambiguous format (ie dd-mm-yyyy vs mm/dd/yyyy). Even in cases like yours where the input is not ambiguous, it is still a very slow piece of code for what it does because it has to parse the date string without knowing what format to expect.

Recent versions of PHP provide a much better alternative if you're doing date arithmetic. Check out the date_diff() function, as well as other functions from the DateTime class. This class provides much better functionality for handling dates than the old-style PHP techniques you're using.

Note that to get the best from the DateTime class, you're going to need to be running the latest version of PHP - ie PHP 5.3. But since PHP 5.2 is no longer supported, you really ought to make sure you're on 5.3 now anyway.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文