繁体   English   中英

如何在php或mysql中找到递归日期?

[英]How to find recursive dates in php or mysql?

我有这样的数据:

$date = '01-01-2014';
$time = '15:20:00';
$location = 'New Delhi';
$recursive = '1';
.........................
.........................  // other data

$ recursive = 1表示每周,2表示每月。

现在我要做的是如果递归类型是每周一次,则将其添加7天,直到3个月;如果递归类型是每月,则将其添加1个月,直到3个月。

例:1 $ date = '01 -01-2014'和$ recursive ='1'

在上面的示例中,意味着$recursive是每周一次,因此获得一月,​​二月和三月的递归日期。
因此,预期结果是:

01-01-2014, 08-01-2014, 15-01-2014, 22-01-2014, 29-01-2014 (recursiive date in january)
05-02-2014, 12-02-2014, 19-02-2014, 26-02-2014 (recursiive date in february)
05-03-2014, 12-03-2014, 19-03-2014, 26-03-2014 (recursiive date in march)

15-04-2014 2:$ date = 15-04-2014年5月4日,$ recursive = 1然后获得4月,5月和6月的递归日期。

输出:

15-04-2014,22-04-2014,29-04-2014 (recursive date in april)
06-05-2014,13-05-2014,20-05-2014,27-05-2014 (recursive date in may)
03-06-2014,10-06-2014,17-06-2014,24-06-2014 (recursive date in june)

范例3:$ date = 01-01-2014 1月1日,$ recursive = 2然后获得4月,5月和6月的递归日期。

这是每月递归,意味着要增加1个月。

输出:

01-01-2014 
01-02-2014 (recursiive date in february)
01-03-2014 (recursiive date in march)

然后我想将这些日期和其他数据插入数据库表中。

那么如何达到以上目标呢? 我应该在php中编写逻辑还是使用mysql查询?

提前致谢。

旁白:目前,我正在使用这个已接受的答案 但现在我正试图改变这一点。

所以基本上你想要做的就是这样

//you have to have a default time zone set.. so i just did this you should already have it in your .ini file
date_default_timezone_set('America/Los_Angeles');
$date = '01-01-2014';
$time = '15:20:00';
$location = 'New Delhi';
$recursive = '1';

//set your start date and end date
$startdate = date_create($date);
$enddate = date_create($date);
$enddate = date_add($enddate,date_interval_create_from_date_string("3 months"));

//set the interval string
if($recursive == '1'){
  $str = "7 days";
} else {
  $str = "1 month";
}
function recursivefunc($str, $start, $end){
  //if the start is equal or bigger than end pop out.
  $s = date_format($start,"Y/m/d");
  $e = date_format($end,"Y/m/d");
  if(strtotime($s) >= strtotime($e)){
    return 1;
  }
  echo date_format($start,"Y/m/d"), '<br>'; //print out the starting date for each loop
  $newDate = date_add($start,date_interval_create_from_date_string($str)); //increment the start
  recursiveFunc($str, $newDate, $end); //call the function again

}
recursiveFunc($str, $startdate, $enddate); // initial call to the function

这就是我解决的方法。

$date = '01-01-2014';
$location = 'New Delhi';

$start = strtotime("+2 months", strtotime($date));  //start date
$end_date = date("Y-m-t", $start);   // last day of end month
$end = strtotime($end_date);   //last date

/* if recursion type is weekly */
if($json['recurrence'] == "1")
{
    for($dd=$start; $dd<=$end;)
    {
        $new_date = date('Y-m-d', $dd);
        $data[] = array(
            'date' => $new_date,
            'location' => $location
        );
        $dd = strtotime("+7 day", $dd);  // add 7 days
    }
}

if($json['recurrence'] == "2")
{
    for($dd=$start; $dd<=$end;)
    {
        $new_date = date('Y-m-d', $dd);
        $data[] = array(
            'date' => $new_date,
            'location' => $location
        );
        $dd = strtotime("+1 month", $dd);  //add 1 month
    }
}


echo "<pre>";
print_r($data);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM