Dates in php sunday & saturday?

I have one date.

example: $date='2011-21-12' ;

data format: yyyy-dd-mm;

IF date is Saturday or Sunday.

if on Saturday add 2 days to the indicated date.

if Sunday adds 1 day to the specified date.

+4
source share
7 answers

In one line of code:

 if (date('N', $date) > 5) $nextweekday = date('Ym-d', strtotime("next Monday", $date)); 

If the day of the week has a value greater than 5 (Monday = 1, Sat - 6, and Sun - 7), set $ nextweekday to YYYY-MM-DD next Monday.

Editing to add, since the date format cannot be accepted, you need to reformat the date first. Add the following lines above my code:

 $pieces = explode('-', $date); $date = $pieces[0].'-'.$pieces[2].'-'.$pieces[1]; 

This will put the date in Ymd order so strtotime can recognize it.

+11
source

You can use date and strtotime to do this, for example:

 $date = strtotime('2011-12-21'); $is_saturday = date('l', $date) == 'Saturday'; $is_sunday = date('l', $date) == 'Sunday'; if($is_saturday) { $date = strtotime('+2 days', $date); } else if($is_sunday) { $date = strtotime('+1 days', $date); } echo 'Date is now ' . date('Ymd H:i:s', $date); 
+3
source

How about this:

 $date='2011-21-12'; if (date("D", strtotime($date)) == "Sat"){ $new_date = date("Ymd", strtotime("+ 2 days",$date); } else if (date("D", strtotime($date)) == "Sun"){ $new_date = date("Ymd", strtotime("+ 1 day",$date); } 
+2
source

A DateTime object can be really useful for anything. In this case.

 $date = DateTime::createFromFormat('Yd-m', '2011-21-12'); if ($date->format('l') == 'Sunday') $date->modify('+1 day'); elseif ($date->format('l') == 'Saturday') $date->modify('+2 days'); 

If you want to return the date in this format.

 $date = $date->format('Yd-m'); 
+2
source
 $date = '2011-21-12' $stamp = strtotime($date); $day = date("l", $stamp); if ($day == "Saturday"){ $stamp = $stamp + (2*+86400); }elseif($day == "Sunday"){ $stamp = $stamp + 86400; } echo date("Ydm", $stamp); 

The only reason I can think of why this is not working is strtotime not recognizing the data format ...

+1
source

For Sunday date ('w', $ date), 0 is returned, so the simplest test code is:

 if(!date('w',strtotime($date)) { ... } //sunday 
0
source

I just subtracted the difference from 8 and added it to the days.

 if(date('N', strtotime($date)) >= 6) { $n = (8 - date("N",strtotime($date))); $date = date("Ymd", strtotime("+".$n." days"); } 
0
source

Source: https://habr.com/ru/post/1339366/


All Articles