给出一周的数字,例如date -u +%W
,你如何计算从周一开始的那一周的天数?
第40周的rfc-3339输出示例:
2008-10-06 2008-10-07 2008-10-08 2008-10-09 2008-10-10 2008-10-11 2008-10-12
ConroyP.. 61
PHP
$week_number = 40; $year = 2008; for($day=1; $day<=7; $day++) { echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n"; }
在PHP中,改编自PHP日期手册页上的这篇文章:
function week_from_monday($date) { // Assuming $date is in format DD-MM-YYYY list($day, $month, $year) = explode("-", $_REQUEST["date"]); // Get the weekday of the given date $wkday = date('l',mktime('0','0','0', $month, $day, $year)); switch($wkday) { case 'Monday': $numDaysToMon = 0; break; case 'Tuesday': $numDaysToMon = 1; break; case 'Wednesday': $numDaysToMon = 2; break; case 'Thursday': $numDaysToMon = 3; break; case 'Friday': $numDaysToMon = 4; break; case 'Saturday': $numDaysToMon = 5; break; case 'Sunday': $numDaysToMon = 6; break; } // Timestamp of the monday for that week $monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year); $seconds_in_a_day = 86400; // Get date for 7 days from Monday (inclusive) for($i=0; $i<7; $i++) { $dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i)); } return $dates; }
输出来自week_from_monday('07-10-2008')
:
Array ( [0] => 2008-10-06 [1] => 2008-10-07 [2] => 2008-10-08 [3] => 2008-10-09 [4] => 2008-10-10 [5] => 2008-10-11 [6] => 2008-10-12 )
小智.. 7
如果你有Zend Framework,你可以使用Zend_Date类来做到这一点:
require_once 'Zend/Date.php'; $date = new Zend_Date(); $date->setYear(2008) ->setWeek(40) ->setWeekDay(1); $weekDates = array(); for ($day = 1; $day <= 7; $day++) { if ($day == 1) { // we're already at day 1 } else { // get the next day in the week $date->addDay(1); } $weekDates[] = date('Y-m-d', $date->getTimestamp()); } echo ''; print_r($weekDates); echo '';
vascowhite.. 6
由于发布了这个问题和接受的答案,DateTime课程使这更容易: -
function daysInWeek($weekNum) { $result = array(); $datetime = new DateTime('00:00:00'); $datetime->setISODate((int)$datetime->format('o'), $weekNum, 1); $interval = new DateInterval('P1D'); $week = new DatePeriod($datetime, $interval, 6); foreach($week as $day){ $result[] = $day->format('D d m Y H:i:s'); } return $result; } var_dump(daysInWeek(24));
这具有照顾闰年等的额外优势.
看它工作.包括困难的第1周和第53周.
PHP
$week_number = 40; $year = 2008; for($day=1; $day<=7; $day++) { echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n"; }
在PHP中,改编自PHP日期手册页上的这篇文章:
function week_from_monday($date) { // Assuming $date is in format DD-MM-YYYY list($day, $month, $year) = explode("-", $_REQUEST["date"]); // Get the weekday of the given date $wkday = date('l',mktime('0','0','0', $month, $day, $year)); switch($wkday) { case 'Monday': $numDaysToMon = 0; break; case 'Tuesday': $numDaysToMon = 1; break; case 'Wednesday': $numDaysToMon = 2; break; case 'Thursday': $numDaysToMon = 3; break; case 'Friday': $numDaysToMon = 4; break; case 'Saturday': $numDaysToMon = 5; break; case 'Sunday': $numDaysToMon = 6; break; } // Timestamp of the monday for that week $monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year); $seconds_in_a_day = 86400; // Get date for 7 days from Monday (inclusive) for($i=0; $i<7; $i++) { $dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i)); } return $dates; }
输出来自week_from_monday('07-10-2008')
:
Array ( [0] => 2008-10-06 [1] => 2008-10-07 [2] => 2008-10-08 [3] => 2008-10-09 [4] => 2008-10-10 [5] => 2008-10-11 [6] => 2008-10-12 )
如果你有Zend Framework,你可以使用Zend_Date类来做到这一点:
require_once 'Zend/Date.php'; $date = new Zend_Date(); $date->setYear(2008) ->setWeek(40) ->setWeekDay(1); $weekDates = array(); for ($day = 1; $day <= 7; $day++) { if ($day == 1) { // we're already at day 1 } else { // get the next day in the week $date->addDay(1); } $weekDates[] = date('Y-m-d', $date->getTimestamp()); } echo ''; print_r($weekDates); echo '';
由于发布了这个问题和接受的答案,DateTime课程使这更容易: -
function daysInWeek($weekNum) { $result = array(); $datetime = new DateTime('00:00:00'); $datetime->setISODate((int)$datetime->format('o'), $weekNum, 1); $interval = new DateInterval('P1D'); $week = new DatePeriod($datetime, $interval, 6); foreach($week as $day){ $result[] = $day->format('D d m Y H:i:s'); } return $result; } var_dump(daysInWeek(24));
这具有照顾闰年等的额外优势.
看它工作.包括困难的第1周和第53周.