我有这个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
我应该如何以及从哪里开始实现这一目标?
// 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);
看到它工作
这将打印日期范围:
$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;