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 use strtotime() and date():
<?php
$s = 'Monday, September 28, 2009';
$time = strtotime($s);
$start = strtotime('last sunday, 12pm', $time);
$end = strtotime('next saturday, 11:59am', $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";
?>
outputs:
Input: Monday, September 28, 2009
Output: Sunday, September 27, 2009 12:00 PM - Saturday, October 3, 2009 11:59 AM
This is what your looking for. It is what I use.
$beginningOfWeek = date('Y-m-d H:i:s', strtotime('last Sunday'));
$endOfWeek = date('Y-m-d H:i:s', strtotime('+6 day', strtotime('last Sunday')) );
strtotime
$input = strtotime("Monday, September 28, 2009");
$nextSunday = strtotime("next Sunday",$input);
<?php
$date = strtotime('Monday, September 28, 2009 - 1 day');
$initialString = date('l, F d, Y g:i A', $date);
$end = date('l, F d, Y g:i A', strtotime( 'next saturday 11:59 pm', $date));
echo $initialString . ' - ' . $end;
output: Sunday, September 27, 2009 12:00 AM - Saturday, October 03, 2009 11:59 PM
<?php
date_default_timezone_set('Europe/London');
$datetime = new DateTime("2009-09-23 12:00:00");
// Saturday
$datetime->setISODate(2009, $datetime->format("W"), 6);
print "Saturday:" . $datetime->format(DATE_ATOM) . "\n";
// Sunday
$datetime->setISODate(2009, $datetime->format("W"), 0);
print "Sunday: " . $datetime->format(DATE_ATOM) . "\n";
?>
echo date("Y-m-d H:i:s", strtotime("last sunday", time()));
echo date("Y-m-d H:i:s", strtotime("next saturday", time()));