views:

775

answers:

6

Hi Guys,

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:

Sunday, September 27, 2009 12:00 AM - Saturday, October 3, 2009 11:59 PM

I am thinking of using the date, strtotime, mktime and time php functions.

If you have a usable function then it is also fine with me.

Thanks in advance :)

Cheers, Mark

A: 

take a look at strtotime

i.e.

strtotime('next sunday', strtotime($your_date_string));
RageZ
A: 

strtotime

$input = strtotime("Monday, September 28, 2009");
$nextSunday = strtotime("next Sunday",$input);
Palo Verde
A: 
<?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

meder
+5  A: 

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
cletus
Thanks for this sir cletus. I think this is much readable and easier to use in my opinion :)
marknt15
+4  A: 
<?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";


?>
RC
Nice, DateTime is relatively new - no?
meder
@meder: Available by default as of 5.2, released in late 2006. It's available on PECL for 5.1.
Charles
A: 

echo date("Y-m-d H:i:s", strtotime("last sunday", time()));

echo date("Y-m-d H:i:s", strtotime("next saturday", time()));

garrizaldy