How can I get the Sunday and Saturday of the week given a specific date?
For example:
input: Monday, September 28, 2009
output should be:
You may find my Date Time Helper much handy in this case , it can finish in 3 Lines
http://normandqq.github.io/Date-Time-Helper/
$date = '2013-08-11';
$monday = Model_DTHpr::getMondayByDate($date);
$sunday = Model_DTHpr::adjustDays("add",$monday,6);
Out Put:
2013-08-05 //Monday
2013-08-11 //Sunday
Get Sunday Php Code:
echo "Today Is : " . date("d-m-Y");
$dw = date( "w");
$dw = 7-$dw;
echo "\nNext Sunday Is" .date('d-m-Y', strtotime("+$dw days"));
take a look at strtotime
i.e.
strtotime('next sunday', strtotime($your_date_string));
the issue with the 'correct' answer is, what if the supplied date is Sunday? Then it will give you the previous Sunday, instead of the current Sunday. Same issue with Saturday.
$s = 'Monday, September 28, 2009';
$time = strtotime($s);
if(date('D', $time) == "Sun"){
$start = strtotime(' 12pm', $time);
}
else {
$start = strtotime('last sunday, 12pm', $time);
}
if(date('D', $time) == "Sat"){
$start = strtotime(' 12pm', $time);
}
else {
$start = strtotime('next saturday, 12pm', $time);
}
$format = 'l, F j, Y g:i A';
$start_day = date($format, $start);
$end_day = date($format, $end);
header('Content-Type: text/plain');
echo "Input: $s\nOutput: $start_day - $end_day";