PHP strtotime() 本周星期一的不同结果

发布于 2024-10-20 19:33:17 字数 287 浏览 10 评论 0原文

我在获取本周星期一的日期时遇到问题。

echo date('Y-m-d',strtotime('monday this week'));

当我在本地计算机(PHP 5.3)上运行上述代码时,它正确输出“2011-03-07”,但我的服务器(PHP 5.2)上的相同代码输出“2011-03-14”(下周一)星期)。

我尝试在两台机器上运行 date('W') 并得到相同的结果 (10)。

编辑:有什么想法可以让这项工作正确进行吗?

提前致谢。

I have a problem getting the date of monday in the current week.

echo date('Y-m-d',strtotime('monday this week'));

When I'm running the above code on my local machine (PHP 5.3) it outputs correctly '2011-03-07', but the same code on my server (PHP 5.2) outputs '2011-03-14' (that's monday next week).

I've tried to run date('W') on both machines and I get the same result (10).

Edit: Any ideas how get this work correctly?

Thanks in advance.

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

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

发布评论

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

评论(8

極樂鬼 2024-10-27 19:33:17

使用
date('Ymd',strtotime(date('o-\\WW')));

Use
date('Y-m-d',strtotime(date('o-\\WW')));

一笔一画续写前缘 2024-10-27 19:33:17

并不是说我确实见过这个问题,但我见过非常相似的问题。 strtotime 似乎改变了不同版本的 PHP 之间的行为,并且除非进行简单的操作(例如“+1 周”),否则很难保证功能保持不变。

如果您无法升级到 5.3(正如正确指出的那样,这似乎是一项改进),我所能建议的就是进行一些排列。通过重新表述问题,通常可以从旧版本的 strtotime 中得到正确的答案。示例可能包括...

  • 下周一 -1 周
  • 上周一 +1 周
  • 本周一

Not that I've seen exactly this problem, but I've seen ones very similar. strtotime seems to change behaviour between different versions of PHP, and barring the simple operations (eg. '+1 week'), it's difficult to guarantee that functionality will remain the same.

If you're not able to upgrade to 5.3, which as correctly pointed out does seem to be an improvement, all I can recommend, is playing with some permutations. It's often possible to get the right answer out of older versions of strtotime by rephrasing the question. Examples might include ...

  • next monday -1 week
  • last monday +1 week
  • this monday
盛夏尉蓝 2024-10-27 19:33:17

是的,strtotime 中字符串的解释在 5.3 中得到了显着改善。

Yes, interpretation of strings in strtotime improved significantly in 5.3.

话少心凉 2024-10-27 19:33:17

从服务器和本地桌面上的 cli 提示符运行命令“date”。这将验证操作系统在两个系统上是否看到相同的日期。

Run the command "date" from the cli prompt on the server and your local desktop. This will verify if the OS is seeing the same date on both systems.

羁〃客ぐ 2024-10-27 19:33:17

您有两种选择:要么避免使用此功能来检查日期是否是未来的日期,如果是,则将其减少 7 天。欢迎来到 PHP 的奇妙世界。

You have two choices: either avoid using this function of check if the date is in the future, if yes you decement it by 7 days. Welcome to the wolderful world of PHP.

携余温的黄昏 2024-10-27 19:33:17

看一下这个:

“请注意,您不能单独依赖此函数来验证日期,因为它会接受像 2 月 31 日这样疯狂的日期。

此外,‘...周’功能本身可能无法达到您的预期如果在周日使用,“下周”将不会返回下周一的时间戳,而是返回下周一的时间戳。同样,当使用“上一周/上周”时,将返回本周周一的时间戳。和“本周”返回下一周(即第二天)的星期一的时间戳,这不是“星期从星期日开始”的效果,因为这意味着返回的所有时间戳都必须是星期日和。他们都不是。”

如果今天是周日,那你就错了。

Look at this:

"Be aware that you cannot rely on this function alone to validate a date, as it will accept insane dates like the 31st of February.

Also, the '... week' functionality by itself may not do what you expect. If used on a Sunday, 'next week' will not return a timestamp of the next Monday, but of the Monday after that. Similarly, a timestamp for the Monday of the current week is returned when 'previous/last week' is used and 'this week' returns a stamp of the Monday of the next week (i.e. the following day). This is not the 'week starts on Sunday' effect, as that would mean all the timestamps returned would have to be on a Sunday and none of them are."

Ff today is sunday, you will be wrong.

情感失落者 2024-10-27 19:33:17

这是另一种有效的解决方案(实际上是唯一对我有用的解决方案):

$mondayOfWeek = date ('d M Y', strtotime($someDate) - ((date ('N', strtotime($someDate)) - 1) * 3600 * 24));

Here is another solution that works (actually the only one that worked for me):

$mondayOfWeek = date ('d M Y', strtotime($someDate) - ((date ('N', strtotime($someDate)) - 1) * 3600 * 24));
不再见 2024-10-27 19:33:17
$today = strtotime('This week monday 8 am');
echo $this->unix_timestamp_to_human($today,"D M j G:i:s T Y");
$today = strtotime('This week monday 8 am');
echo $this->unix_timestamp_to_human($today,"D M j G:i:s T Y");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文