[ACCEPTED]-PHP: How to get Sunday and Saturday given a date input?-datetime

Accepted answer
Score: 22

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

0

Score: 9
<?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";


?>

0

Score: 3

take a look at strtotime

i.e.

strtotime('next sunday', strtotime($your_date_string));

0

Score: 1

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

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

Score: 1

the issue with the 'correct' answer is, what 3 if the supplied date is Sunday? Then it 2 will give you the previous Sunday, instead 1 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";
Score: 0

strtotime

$input = strtotime("Monday, September 28, 2009");
$nextSunday = strtotime("next Sunday",$input);

0

Score: 0
<?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 1 AM - Saturday, October 03, 2009 11:59 PM

Score: 0

This is what your looking for. It is what 1 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')) );
Score: 0

You may find my Date Time Helper much handy 2 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 1 Put:

    2013-08-05 //Monday
    2013-08-11 //Sunday
Score: 0

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")); 

0

More Related questions