How do I return the previous Sunday from 7 days ago using PHP date()?

别来无恙 提交于 2019-12-11 08:39:50

问题


Here is what I have so far:

$date = date('Y-m-d h:i:s', strtotime('-7 days')); 
$start = date('Y-m-d h:i:s', strtotime($date,'previous Sunday'));

When outputting $start, it returns: 1969-12-31 06:00:00

What am I doing wrong?


回答1:


$date needs to e a timestamp

$date = strtotime('-7 days'); 
$start = date('Y-m-d h:i:s', strtotime('previous Sunday',$date));



回答2:


You have the arguments the wrong way round:

date('Y-m-d h:i:s', strtotime('previous Sunday', $date));

Edit: Furthermore, you have made $date a formatted string. It needs to be a timestamp, so your code should look something like this:

$date = strtotime('-7 days'); 
$start = date('Y-m-d h:i:s', strtotime('previous Sunday', $date));



回答3:


Per php doc

date('Y-m-d h:i:s', strtotime('last Sunday', $date));



回答4:


If your date is not a timestamp you can still use strtotime, like suppose your date was passed in already and is in a string format of another kind.

$date = '2013-11-10';
$lastsunday = date('Y-m-d',strtotime($date.' last Sunday'));

This can save a bit of time trying to get your date into a format that "works"



来源:https://stackoverflow.com/questions/4730079/how-do-i-return-the-previous-sunday-from-7-days-ago-using-php-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!