在 PHP 中将一周中的某一天转换为 Unix 时间

发布于 2024-08-27 01:21:28 字数 225 浏览 8 评论 0原文

我试图获取格式如下的日期字符串的 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 技术交流群。

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

发布评论

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

评论(2

栀子花开つ 2024-09-03 01:21:28

这应该可以通过 strtotime 实现。您可以尝试使用 mktime() 生成三月第一天的时间戳并将其添加为第二个参数(在字符串部分中仅保留“第一个星期日”):

$timestamp = mktime (0,0,0,3,1,2010);  // 1st of march
$first_sunday = strtotime("first sunday", $timestamp);

不确定这将如何处理第一天(3 月 1 日)实际上是星期日。请务必检查一下。

另外,也许这更可靠,请检查这个 -发帖者说他用以下符号(引用)得到了很好的结果:

<?php
strtotime('+0 week sun nov 2009'); // first sunday in nov 2009
strtotime('+1 week sun nov 2009'); // second sunday
strtotime('-1 week sun nov 2009'); // last sunday in oct 2009
?>

与 strtotime 一样,无论您选择什么,请确保测试良好,尤其是边缘情况(每月的第一天是星期日,上个月的最后一天是星期日....)

This should be possible with strtotime. You could try generating a timestamp of the first day of march using mktime() and adding that as a 2nd parameter (leaving just "first sunday" in the string part):

$timestamp = mktime (0,0,0,3,1,2010);  // 1st of march
$first_sunday = strtotime("first sunday", $timestamp);

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

<?php
strtotime('+0 week sun nov 2009'); // first sunday in nov 2009
strtotime('+1 week sun nov 2009'); // second sunday
strtotime('-1 week sun nov 2009'); // last sunday in oct 2009
?>

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

月亮坠入山谷 2024-09-03 01:21:28

你的代码适用于 PHP 5.3.0。您使用什么版本的 PHP?

<?php
date_default_timezone_set("Europe/Oslo");
$time_march = strtotime('second sunday of march 2010');
$time_november = strtotime('first sunday of november 2010');
echo date("Y-m-d", $time_march) . " (timestamp: $time_march)\n";
echo date("Y-m-d", $time_november) . " (timestamp: $time_november)\n";
?>

给出:

2010-03-14 (timestamp: 1268521200)
2010-11-07 (timestamp: 1289084400)

Your code works for me on PHP 5.3.0. What version of PHP are you using?

<?php
date_default_timezone_set("Europe/Oslo");
$time_march = strtotime('second sunday of march 2010');
$time_november = strtotime('first sunday of november 2010');
echo date("Y-m-d", $time_march) . " (timestamp: $time_march)\n";
echo date("Y-m-d", $time_november) . " (timestamp: $time_november)\n";
?>

gives:

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