PHP: How to get Sunday and Saturday based on date input?

How can I get Sunday and Saturday of the week with a specific date?

For example:

Admission: Monday, September 28, 2009

the conclusion should be:

Sunday, September 27, 2009 12:00 - Saturday, October 3, 2009 23:59

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

If you have a useful feature, then this is also good.

Thanks in advance:)

Cheers, Mark

+6
algorithm php datetime
source share
10 answers

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 
+16
source share
 <?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"; ?> 
+9
source share

take a look at strtotime

i.e.

 strtotime('next sunday', strtotime($your_date_string)); 
+3
source share

echo date ("Ymd H: i: s", strtotime ("last Sunday", time ()));

echo date ("Ymd H: i: s", strtotime ("next Saturday", time ()));

+1
source share

The question with the β€œcorrect” answer is, what if the delivered date is Sunday? Then it will give you the previous Sunday instead of the current Sunday. The same question is 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"; 
+1
source share

Strtotime

 $input = strtotime("Monday, September 28, 2009"); $nextSunday = strtotime("next Sunday",$input); 
0
source share
 <?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; 

Exit: Sunday, September 27, 2009 12:00 - Saturday, October 03, 2009 23:59

0
source share

This is what you are looking for. This is what I use.

 $beginningOfWeek = date('Ymd H:i:s', strtotime('last Sunday')); $endOfWeek = date('Ymd H:i:s', strtotime('+6 day', strtotime('last Sunday')) ); 
0
source share

In this case, you can find my assistant Time Time Helper, it can end in 3 lines

http://normandqq.imtqy.com/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 
0
source share

Get Sunday Php Code:

 echo "Today Is : " . date("dmY"); $dw = date( "w"); $dw = 7-$dw; echo "\nNext Sunday Is" .date('dm-Y', strtotime("+$dw days")); 
0
source share

All Articles