Reputation: 779
With the code I have now, it managed to return dates way off in 1969 and 1970.Example of $endDate is 23-8-2013. Day, month, year.
$endDte = date_create_from_format('j-n-Y', $endDate);
echo date('Y-m-d', strtotime('-1 Sunday', strtotime($endDte))). date('Y-m-d', strtotime('+1 Saturday', strtotime($endDte)));
This code doesn't seem to give me a previous Sunday and the next Saturday relative to the date given so I'm wondering if I'm doing something wrong.
Upvotes: 0
Views: 95
Reputation: 70460
date_create_from_format
returns a DateTime
object, not a string you should run through strtotime
...:
$date = date_create_from_format('j-n-Y', $endDate);
$startdate = clone $date;
$startdate->modify('-1 Sunday');
$enddate = clone $date;
$enddate->modify('+1 Saturday');
echo $startdate->format('Y-m-d').' => '.$enddate->format('Y-m-d');
Upvotes: 2
Reputation: 2988
I think you're looking for last sunday
and next saturday
, instead of -1 sunday
and +1 saturday
Then I'd wrap them into functions, an example for the last sunday:
function lastSunday($ts=time(), $weeksApart=1){
if(date('N', $ts) == 7){
$time = $ts;
} else{
$time = strtotime('last sunday', $ts);
}
return date('Y-m-d', strtotime('+'.$weeksApart.' weeks', $ts));
}
Upvotes: 0