PHP函数在两个发布日期之间循环工作日 [英] PHP function to loop business days between two posted dates

查看:119
本文介绍了PHP函数在两个发布日期之间循环工作日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

打印使用PHP从MySQL开始的两个日期之间的业务日期列表Ymd列表


我很喜欢这些PHP编程,所以我忍受。



我希望用户能够发布两个日期,一个开始日期和一个结束日期,并从这些日期计算业务日期(不包括星期六和星期日),并将这些日期插入到 MySQL



例如,如果我有这个表单...

 < form action ='update.php'method ='post'> 
< input type ='text'name ='start_date'>
< input type ='text'name ='end_date'>
< input type ='submit'name ='submit'>
< / form>

update.php


$ b $如果(isset($ _ POST ['submit'])){pre> ){

$ start_date = $ _POST ['start_date']; //让我们说这是2012-10-01
$ send_date = $ _POST ['end_date']; //让我们说这是2012-10-10

}

现在我想循环遍历日期并创建一个MySQL查询,运行如下:

  INSERT INTO tbl_dates(date_value)VALUES ('2012-10-01'); 
INSERT INTO tbl_dates(date_value)VALUES('2012-10-02');
INSERT INTO tbl_dates(date_value)VALUES('2012-10-03');
INSERT INTO tbl_dates(date_value)VALUES('2012-10-04');
INSERT INTO tbl_dates(date_value)VALUES('2012-10-05');
INSERT INTO tbl_dates(date_value)VALUES('2012-10-08');
INSERT INTO tbl_dates(date_value)VALUES('2012-10-09');
INSERT INTO tbl_dates(date_value)VALUES('2012-10-10');

我知道这可能不是一个很好的解决方案,但这是我需要的。



如何创建循环?

解决方案

  $ startDate = $ start_date; 
$ endDate = $ send_date;

//转换为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);
}

//开始插入
foreach($ result as $ value)
{
mysql_query(INSERT INTO tbl_dates(date_value)VALUES' $ value。');
}

资料来源:打印使用PHP的MySQL的两个日期之间的业务日期列表YMD列表



Omg!我只是意识到这是你的一个线程。你已经有了你的答案


Possible Duplicate:
Print Y-m-d list of business dates between two dates from MySQL using PHP

I'm new to these kind of PHP programming so bear with me.

I want users to be able to post two dates, a startdate and a enddate and from these dates calculate the business dates (exclude Saturday and Sunday) and insert these to MySQL

For example if I have this form...

<form action='update.php' method='post'>
  <input type='text' name='start_date'>
  <input type='text' name='end_date'>
  <input type='submit' name='submit'>
</form>

update.php

if(isset($_POST['submit'])) {

   $start_date = $_POST['start_date']; //Let's say this is 2012-10-01
   $send_date = $_POST['end_date']; //Let's say this is 2012-10-10

}

Now I would like to loop through the dates and create a MySQL query that would run like this:

INSERT INTO tbl_dates (date_value) VALUES ('2012-10-01');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-02');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-03');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-04');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-05');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-08');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-09');
INSERT INTO tbl_dates (date_value) VALUES ('2012-10-10');

I know this is probably not a good solution but it's how I need it.

How do I create the loop?

解决方案

$startDate = $start_date;
$endDate = $send_date;

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

// start insertion
foreach($result as $value)
{
  mysql_query("INSERT INTO tbl_dates (date_value) VALUES '".$value."'");
}

Source : Print Y-m-d list of business dates between two dates from MySQL using PHP

Omg! I just realized that it's a thread started by you. You already have your answer

这篇关于PHP函数在两个发布日期之间循环工作日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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