问题
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