gpt4 book ai didi

php - 使用 strtotime 获取一个月中的第一个工作日

转载 作者:可可西里 更新时间:2023-10-31 22:08:40 25 4
gpt4 key购买 nike

我试图使用 strtotime 计算出给定月份的第一个星期三,但是只要第一个星期三恰好是 1 日,“第一个星期三”参数就会失败。

有关此问题的更一般说明,请参见以下代码和结果:

$mon = strtotime("December 2010 first monday");
$tue = strtotime("December 2010 first tuesday");
$wed = strtotime("December 2010 first wednesday");
$thu = strtotime("December 2010 first thursday");
$fri = strtotime("December 2010 first friday");
$sat = strtotime("December 2010 first saturday");
$sun = strtotime("December 2010 first sunday");

echo strftime("%m/%d/%y", $mon) . "<br>";
echo strftime("%m/%d/%y", $tue) . "<br>";
echo strftime("%m/%d/%y", $wed) . "<br>";
echo strftime("%m/%d/%y", $thu) . "<br>";
echo strftime("%m/%d/%y", $fri) . "<br>";
echo strftime("%m/%d/%y", $sat) . "<br>";
echo strftime("%m/%d/%y", $sun) . "<br>";

结果:

12/06/10
12/07/10
12/08/10
12/02/10
12/03/10
12/04/10
12/05/10

注意到什么了吗?一周中的某一天不应该与一个月的第一天重合吗?但它永远不会这样做,而是总是返回当天的第二个实例,即 8 号。

有人对此有解释吗?

最佳答案

根据所提供的答案,需要了解 php 5.2 和 php 5.3 之间相对日期格式的差异。

测试:

$date1 = strtotime("first wednesday of 2010-12");
$date2 = strtotime("first wednesday 2010-12");
$date3 = strtotime("wednesday 2010-12");

5.2 结果:

1969-12-31
2010-12-08
2010-12-01

5.3 结果:

2010-12-01
2010-12-08
2010-12-01

因此只有第三种方法在 PHP 5 版本中返回正确的结果。

关于php - 使用 strtotime 获取一个月中的第一个工作日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4357490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com