开发者

How to get weekend day in this week by any given date?

for example, get the开发者_如何学编程 weekend of today.


If you're looking for e.g. the next or previous saturday (or any other weekday for that matter) strtotime is your friend.

$prev = strtotime('-1 saturday');    
$next = strtotime('saturday');

var_dump($prev, $next);

It's worth noting that strtotime is quite an expensive function, so multiple calculations will noticiably add to your execution time. A good compromise is using it to get a starting point and using the other date functions to derive further dates.


This is brilliantly easy in PHP:

<?php

echo date( 'Y-m-d', strtotime( 'next Saturday' ) );

?>


get the current date.
get the current day of week. (0=monday, 6 = sunday)
days2weekend = 5 - current day of week 
dateadd(currentdate, days, days2weekend)


Your question is a bit a hard to follow, but do you mean "the next weekend" from a certain date?

You could get the the weekday number, and then see how much to add for saturday and sundag? That would look like:

<?php   
    $dayNR = date('N');         //monday = 1, tuesday = 2, etc.
    $satDiff = 6-$dayNR;        //for monday we need to add 5 days -> 6 - 1
    $sunDiff = $satDiff+1;      //sunday is one day more
    $satDate = date("Y-m-d", strtotime(" +".$satDiff." days"));
    $sunDate = date("Y-m-d", strtotime(" +".$sunDiff." days"));

    echo $satDate."\n";
    echo $sunDate."\n";
?>


I think the built-in PHP function strtotime should work for you. Eg:

strtotime('next saturday')

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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜