我有这个MySQL
表:
desc studentabsence;
+---------------------------+-------------+
| Field | Type |
+---------------------------+-------------+
| student_id | INT(11) |
| student_absence_startdate | date |
| student_absence_enddate | date |
+---------------------------+-------------+
我们说我们有
student_absence_startdate = 2012-08-01
student_absence_enddate = 2012-08-08
使用PHP,我想echo
该范围之间的所有工作日(周一至周五)。
从上述范围我想打印:
2012-08-01
2012-08-02
2012-08-03
2012-08-06
2012-08-07
2012-08-08
我应该如何以及在何处开始实现这一目标?
答案 0 :(得分:3)
// Date strings from DB
$startDate = '2012-08-01';
$endDate = '2012-08-08';
// Convert to UNIX timestamps
$currentTime = strtotime($startDate);
$endTime = strtotime($endDate);
// Loop until we reach the last day
$result = array();
while ($currentTime <= $endTime) {
if (date('N', $currentTime) < 6) {
$result[] = date('Y-m-d', $currentTime);
}
$currentTime = strtotime('+1 day', $currentTime);
}
// Show the result
// You could loop the array to pretty-print it, or do it within the above loop
print_r($result);
答案 1 :(得分:2)
这将打印日期范围:
$startDate = '2012-08-01';
$endDate = '2012-08-08';
$date = new DateTime($startDate);
while ($date->format('Y-m-d') != $endDate) {
if ($date->format('N') > 5) {
$date->modify('+1 day');
continue;
}
echo $date->format('Y-m-d') . PHP_EOL;
$date->modify('+1 day');
}
echo $endDate;