如何在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)

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...