Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dates in php Sunday and Saturday?

Tags:

date

php

i have one date.

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

data format :yyyy-dd-mm;

IF date is Saturday or Sunday.

if Saturday add 2 day to the given date.

if Sunday add 1 day to the given date. ?

like image 645
Karthik Avatar asked Dec 01 '22 03:12

Karthik


1 Answers

In a single line of code:

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

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

Editing to add, because the date format may not be accepted, you would 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 Y-m-d order so that strtotime can recognize it.

like image 69
Wige Avatar answered Dec 04 '22 09:12

Wige