在 PHP 中将一周中的某一天转换为 Unix 时间
我试图获取格式如下的日期字符串的 unix 时间:
'second sunday of march 2010'
'first sunday of november 2010'
我的印象是 strtotime 可以处理这样的字符串,但显然不能,因为这会返回 false。当给定一周中的某一天、该月中的哪一天(即第一、第二等)、一个月和一年时,如何转换为 Unix 时间。
I'm trying to get the unix time for date strings that are formatted like so:
'second sunday of march 2010'
'first sunday of november 2010'
I was under the impression that strtotime could handle such a string, but apparently not, as this returns false. How can I convert to unix time when given a day of week, which one of those in the month (ie. first, second, etc.), a month and a year.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以通过
strtotime
实现。您可以尝试使用 mktime() 生成三月第一天的时间戳并将其添加为第二个参数(在字符串部分中仅保留“第一个星期日”):不确定这将如何处理第一天(3 月 1 日)实际上是星期日。请务必检查一下。
另外,也许这更可靠,请检查这个 -发帖者说他用以下符号(引用)得到了很好的结果:
与 strtotime 一样,无论您选择什么,请确保测试良好,尤其是边缘情况(每月的第一天是星期日,上个月的最后一天是星期日....)
This should be possible with
strtotime
. You could try generating a timestamp of the first day of march usingmktime()
and adding that as a 2nd parameter (leaving just "first sunday" in the string part):Not sure how this will handle the first day (March 1st) actually being a sunday. Make sure you check that out.
Also, and maybe this more reliable, check this out - the poster says he got good results with the following notation (quoting):
As always with strtotime, whatever you pick, make sure you test well, especially the edge cases (1st day of month is a sunday, last day of last month was a sunday....)
你的代码适用于 PHP 5.3.0。您使用什么版本的 PHP?
给出:
Your code works for me on PHP 5.3.0. What version of PHP are you using?
gives: