php - 使用 PHP 从 MySQL 打印两个日期之间的 Y-m-d 业务日期列表

标签 php mysql date between

我有这个 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,我想回显该范围(周一至周五)之间的所有工作日。

从上面的范围我想打印:

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);

See it working

关于php - 使用 PHP 从 MySQL 打印两个日期之间的 Y-m-d 业务日期列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11904033/

相关文章:

php - 将 .php 页面加载到 div Ajax 和 Javascript

c# - '正则表达式' VS 'String Comparison operators/functions'

mysql - Ecto 为 Mysql/Mariadb 创建唯一索引失败

php - 如何使用匿名函数和闭包正确设置 PDO 连接

php - 使用 ajax 函数根据下拉列表更改 select 语句的表?

sql - PostgreSQL:按随机天数更改日期

vba - excel 说 6 月的日期大于 7 月的日期 - 为什么?

长度为 32 的 PHP 空字符串

php - 来自 mysql 查询的 foreach 循环

ios - 将字符串转换为 NSDate 给出错误的日期