PHP - 想要在任何给定日期返回上周日的日期


PHP - Want to return the date of last Sunday in any given day

我想在我的网站上创建一个"本周图片",并让它从周日到周日动态更改。因此,一旦下一个星期天到来,它就会变成那一天。这是我到目前为止得到的:

function getLastSunday(){
if (1==1)
{
$today = date('F j Y');
$lastSunday = date('m/d/Y', strtotime('last Sunday', $today));
}
return $lastSunday;
}

它返回"01/01/1970"。我希望它返回"10/13/2013",下周我希望它返回"10/20/2013"

你正在传递一个字符串作为第二个参数给strtotime(),根据 PHP 文档,它确实需要几秒钟。

把它当作一个时间(或者甚至只是忽略它?)应该让它更好地工作:

function getLastSunday(){
if (1==1)
{
$today = date('F j Y');
$lastSunday = date('m/d/Y', strtotime('last Sunday', strtotime($today)));
}
return $lastSunday;
}

你真正想要的是这个,虽然我认为:

function getLastSunday() {
    return date('m/d/Y', strtotime('last Sunday'));
}