marknt15 marknt15 - 3 months ago 8
PHP Question

PHP: How to get Sunday and Saturday given a date input?

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

Answer

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
Comments