我希望能够在给定日期找到“星期日”的日期,格式为YYYY-MM-DD。
......如何在PHP中实现这一目标?
答案 0 :(得分:1)
strtotime()可以帮助你,它接受GNU-Date-input格式,如"next Sunday"
echo date('Y-m-d',strtotime('sunday',strtotime("2011-07-04 Z")));
答案 1 :(得分:0)
查看php的time函数。
答案 2 :(得分:0)
如果找不到执行此操作的库,则:
答案 3 :(得分:0)
这里有一个包含倍数值的完整示例 它适用于使用纪元时间(或类似)的PHP或任何其他语言
<?php
function prevSunday($paramDay){
$aDay = 24*60*60;
$aWeek = $aDay * 7;
$sunday = $aDay + $paramDay - (($paramDay - 3 * $aDay) % $aWeek);
return $sunday;
}
$randDay = 1309819423 + rand(0, 9000000); // this contanis a random day (and hours, minutes, seconds)
$aSaterday = strtotime("20110702");
$aSunday = strtotime("20110703");
$aMonday = strtotime("20110704");
echoTwoDays($aSuterday, prevSunday($aSuterday));
echoTwoDays($aSunday, prevSunday($aSunday));
echoTwoDays($aMonday, prevSunday($aMonday));
/* echoes
day1: Saturday 20110702
day2: Sunday 20110626
day1: Sunday 20110703
day2: Sunday 20110703
day1: Monday 20110704
day2: Sunday 20110703
*/
echoTwoDays($randDay, prevSunday($randDay)); // echoes a random date and perv sunday
function echoTwoDays ($day1, $day2) {
echo "day1: " . date("l Ymd", $day1) ."<br>"; // remove l as your request
echo "day2: " . date("l Ymd", $day2) ."<br><br>"; // remove l as your request
}
function prevSunday($paramDay){
$aDay = 24*60*60;
$aWeek = $aDay * 7;
$sunday = $aDay + $paramDay - (($paramDay - 3 * $aDay) % $aWeek);
return $sunday;
}
?>