如何在PHP中获得一年的最后一个星期日


How to get the last Sunday of a year in PHP

我需要为一年的最后一个星期日创建一个到期日期。我知道我可以使用strtotime函数来传递"上周日"之类的内容,这可以应用于当前年份吗?

使用strtotime您可以使用'last' space dayname space 'of' monthname,这是相对于当前年份的

strtotime('last sunday of december')

有关可用的格式,请参阅相对格式文档

在线演示/沙盒测试

一种方法:

echo "Last Sunday of 2013 is: ".date('Y-m-d', strtotime('01/01/2014'.' last Sunday'));

这将产生

Last Sunday of 2013 is: 2013-12-29

一周中所有天的沙盒测试

更新

:代码已更新以正确指出@hakre解决问题。无论如何,正确的方法可能是@Austin Brunkhorst建议的。