开发者

Ignore/skip weekends with strtotime

开发者 https://www.devze.com 2023-03-30 11:51 出处:网络
I\'m using strtotime within a whil开发者_Go百科e statment to automatically update entries in a database.

I'm using strtotime within a whil开发者_Go百科e statment to automatically update entries in a database.

It adds on an amount of days depending on what the user has stated in the previous page.

Using this code:

    $calendardate = strtotime ( '+'.$calendarday.' day' , strtotime ( $date ) ) ;
    $calendardate = date ( 'Y-m-d' , $calendardate );

How can I get it to skip weekends? I need to use the same variable ($calendardate) but I need it to ignore Saturdays and Sundays.

Any help is most appreciated :)


$now = date("Y-m-d H:i:s");
$h = date("H:i:s");    
$two_weekdays_later = strtotime(date("Y-m-d H:i:s", strtotime($now)) . " +2 weekdays $h");


In the end I settled on iterating day by day, testing if something is a Saturday or Sunday. Otherwise it's too hard to account for end of year etc. (granted I had other requirements too, like skipping other days than just weekends, but likely you'll end up with the same requirements one day - like skipping holidays etc.).


Use the date function as follows:

if ((date("w", $calendardate) == 0) || (date("w", $calendardate) == 6))
{ 
  echo "weekend";
}

Sunday = 0, Saturday = 6

If you use date("N", $calendardate), then the numbers run for 1 = "Monday" through 7 = "Sunday"

http://php.net/manual/en/function.date.php

Note that $calendardate needs to be a unix timestamp as per the specification in the link.

0

精彩评论

暂无评论...
验证码 换一张
取 消