[英]1st I converted date to week number ( 01 to 53 ) in PHP and now i don't know how to get week number to first_date of week
Here a simple code of date to week_number of year and now. 这里是一个简单的日期代码,表示现在的year_number和week_number。 How can I get start_date and end_date of week_number
如何获取week_number的开始日期和结束日期
$date_string = "2012-12-30";
echo "Weeknummer: " . date("W", strtotime($date_string));
You can use this: 您可以使用此:
<?php
$date_string = "2012-12-30";
$weekNumber = date("W", strtotime($date_string));
echo "Weeknummer: ".$weekNumber;
echo '</br>';
// get the year
$year = date("Y", strtotime($date_string));
// set the date string for the week number
$dateWeek = $year.'-W'.$weekNumber;
// increase the weekNumber to the next
$weekNumber = intval($weekNumber);
$weekNumber += 1;
// if it is lower than 10 add preceeding 0
if($weekNumber < 10) $weekNumber = '0'.$weekNumber;
// set the date string for the next week number
$dateWeekNext = $year.'-W'.$weekNumber;
echo '</br>';
// get the first day of the week
echo date('Y-m-d', strtotime($dateWeek));
echo '</br>';
// get the day before the first day of the next week
echo date('Y-m-d', strtotime($dateWeekNext . ' -1 day'));
?>
This outputs: 输出:
Weeknummer: 52
2012-12-24
2012-12-30
There is many ways to do it, here is one. 有很多方法可以做到,这是一种。
I use "N" of date to determine what day the selected day is and use strtotimes way of understanding simple text to find previous or next Monday/Sunday. 我使用日期的“ N”来确定所选日期是星期几,并使用strtotimes理解简单文本的方式来查找上一个或下一个星期一/星期日。
$date_string = "2018-01-27";
$w =date("W", strtotime($date_string));
$N =date("N", strtotime($date_string));
If($N == 1){ // if monday
$monday = $date_string;
$sunday = date("Y-m-d", strtotime("next Sunday $date_string"));
}Elseif($N ==7){ // if sunday
$monday = date("Y-m-d", strtotime("previous Monday $date_string"));
$sunday = $date_string;
}Else{// any other weekday
$monday = date("Y-m-d", strtotime("previous Monday $date_string"));
$sunday = date("Y-m-d", strtotime("next Sunday $date_string"));
}
echo "Weeknummer: $w.\nMonday: $monday.\nSunday: $sunday.";
Output: 输出:
Weeknummer: 52.
周日:52。
Monday: 2012-12-24.星期一:2012-12-24。
Sunday: 2012-12-30.星期日:2012-12-30。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.