打印使用PHP从MySQL的两个日期之间的业务日期的Y-m-d列表 [英] Print Y-m-d list of business dates between two dates from MySQL using PHP

查看:156
本文介绍了打印使用PHP从MySQL的两个日期之间的业务日期的Y-m-d列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 MySQL 表:

  desc studentabsence; 
+ --------------------------- + ------------- +
| Field |类型|
+ --------------------------- + ------------- +
| student_id | INT(11)|
| student_absence_startdate |日期|
| student_absence_enddate |日期|
+ --------------------------- + ------------- +

让我们说,我们有



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


我应该如何开始实现这一目标?

解决方案

  //从DB 
$ startDate ='2012-08-01'的日期字符串;
$ endDate ='2012-08-08';

//转换为UNIX时间戳
$ currentTime = strtotime($ startDate);
$ endTime = strtotime($ endDate);

//循环直到我们到达最后一天
$ result = array();
while($ currentTime< = $ endTime){
if(date('N',$ currentTime)< 6){
$ result [] = date('Ym-d ',$ currentTime);
}
$ currentTime = strtotime('+ 1天',$ currentTime);
}

//显示结果
//你可以循环数组来漂亮打印它,或者在上面的循环中执行
print_r($ result );

看到它工作


I have this MySQL table:

desc studentabsence;
+---------------------------+-------------+
| Field                     | Type        |
+---------------------------+-------------+
| student_id                | INT(11)     |
| student_absence_startdate | date        |
| student_absence_enddate   | date        |
+---------------------------+-------------+

Let's say that we have

student_absence_startdate = 2012-08-01
student_absence_enddate = 2012-08-08

Using PHP, I would like to echo all business days between that range (Mon-Fri).

From the above range I would like to print:

2012-08-01
2012-08-02
2012-08-03
2012-08-06
2012-08-07
2012-08-08

How and where should I start to achieve this?

解决方案

// 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从MySQL的两个日期之间的业务日期的Y-m-d列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆