PHP strtotime() 本周周一的不同结果 [英] PHP strtotime() different results of monday this week

查看:31
本文介绍了PHP strtotime() 本周周一的不同结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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).

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

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

任何想法如何正确地工作?

Any ideas how get this work correctly?

提前致谢.

推荐答案

并不是说我见过这个问题,但我见过非常相似的问题.strtotime 似乎改变了不同 PHP 版本之间的行为,除非进行简单的操作(例如+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.

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

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 ...

  • 下周一-1周
  • 上周一 +1 周
  • 这个星期一

这篇关于PHP strtotime() 本周周一的不同结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆