How do I return the previous Sunday from 7 days ag

2019-07-23 03:54发布

问题:

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"