我可以用字符串参数创建DateTime
对象来获取日期,就像这样。
$first = new \DateTime("first monday of july 2015");
$second = new \DateTime("last tuesday of july 2015");
$third = new \DateTime("first monday of january 2015");
但是我怎么能得到7月的倒数第二个星期一?
答案 0 :(得分:2)
$penultimateMonday = new \DateTime("last monday of july 2015 -1 week");
echo $penultimateMonday->format('Y-m-d');
<强>输出强>
2015-07-20
答案 1 :(得分:1)
您可以通过DateInterval示例
来完成$last = new \DateTime("last monday of july 2015");
$penultimate = $last->sub(new \DateInterval('P7D'));
变量$倒数第二个将是上周一减7天,即7月倒数第二个星期一
答案 2 :(得分:0)
七月的倒数第二个星期一总是在七月的最后一个星期一前7天,所以你可以这样做:
$last = new \DateTime("last monday of july 2015");
$penultimate = $last -7;
答案 3 :(得分:-1)
echo $test = date("Y-m-d",strtotime("last monday of july 2015"));