微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在PHP中获得一个月的第一,第三和第五个星期六?

我已经找到了解决这个问题的方法,但是在某些情况下它出错了.以下是我的发现:

代码段将打印2015年8月的第一个星期六和2015年7月:

$sat = date("F Y",strtotime("2015-08-01"));
echo $fir_sat = "$sat first saturday";
echo " is ".date('d', strtotime($fir_sat));

$sat = date("F Y",strtotime("2015-07-04"));
echo $fir_sat = "$sat first saturday";
echo " is ".date('d', strtotime($fir_sat));

以下是输出

August 2015 first Saturday is 08
July 2015 first Saturday is 04

但实际上它是01:

August 2015 first Saturday is 01

怎么会发生?解决办法是什么?

根据您的反馈,我尝试了一些编码.以下是我的发现.错误发生是因为PHP版本.

在像5.2.9这样的较低版本的PHP中,以下代码不起作用

回音日期(‘Y-m-d’,strtotime(‘2015年8月的第一个星期六’));

但是在像5.4这样的更高版本中,它正在萎缩

有任何想法吗?

解决方法:

尝试使用as

echo date('d F Y', strtotime('first saturday of $fir_sat'));

“ordinal dayname ‘of’ ” does not advance to another day.

代替

echo date('d F Y', strtotime('$fir_sat first saturday'));

“ordinal dayname ” does advance to another day.

You can check the difference over here

你也可以查看Docs(Notes)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐