[英]How to get weekend day in this week by any given date?
例如,今天的周末。
If you're looking for eg the next or previous saturday (or any other weekday for that matter) strtotime
is your friend. 如果您正在寻找例如下周六或上周六(或任何其他工作日),那么
strtotime
就是您的朋友。
$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. 值得注意的是,
strtotime
是一个非常昂贵的功能,因此多次计算会显着增加您的执行时间。 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中非常简单:
<?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? 你可以得到工作日的数字,然后看看星期六和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. 我认为内置的PHP函数strtotime应该适合你。 Eg:
例如:
strtotime('next saturday')
http://php.net/manual/en/function.strtotime.php http://php.net/manual/en/function.strtotime.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.