Php 如何在给定日期前获得本周的周末?

Php 如何在给定日期前获得本周的周末?,php,Php,例如,今天的周末。你的问题有点难理解,但你是说从某个日期开始的“下一个周末”吗 get the current date. get the current day of week. (0=monday, 6 = sunday) days2weekend = 5 - current day of week dateadd(currentdate, days, days2weekend) 你可以得到工作日的号码,然后看看周六和周日要加多少?这看起来像: <?php $dayN

例如,今天的周末。

你的问题有点难理解,但你是说从某个日期开始的“下一个周末”吗

get the current date.
get the current day of week. (0=monday, 6 = sunday)
days2weekend = 5 - current day of week 
dateadd(currentdate, days, days2weekend)
你可以得到工作日的号码,然后看看周六和周日要加多少?这看起来像:

<?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";
?>

如果您正在寻找下一个或上一个星期六(或任何其他工作日)是您的朋友

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

var_dump($prev, $next);

值得注意的是,
strotime
是一个非常昂贵的函数,因此多次计算会显著增加执行时间。一个很好的折衷方法是使用它来获得一个起点,并使用另一个来派生进一步的日期。

这在PHP中非常容易:

<?php

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

?>

我认为内置的PHP函数strotime应该适合您。例如:

strtotime('next saturday')

您应该提供更多详细信息-您想要在给定日期之后的周末,还是在给定日期之前的周末?如果给你一个星期天,你会怎么做?@lovespring:好吧,那么如果给你一个星期天,你可能会想要下一个星期六和星期天?如果给你一个星期六呢?在问这样的问题时,准确一点是很有帮助的。可能重复的可能重复的可能重复的嗯,实际上取决于你所说的“周末”(周末的周末是周日,“周末”的开始是周六),当你的一周真正结束的时候——还有一些文化,在“一周”的第一个工作日是星期天。这个问题其实没有简单的答案,因为我们不知道是哪种文化,也不知道“周末”是什么意思。。。