Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php或mysql中查找递归日期?_Php_Mysql_Sql_Recursion - Fatal编程技术网

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

如何在php或mysql中查找递归日期?,php,mysql,sql,recursion,Php,Mysql,Sql,Recursion,我有这样的数据: $date = '01-01-2014'; $time = '15:20:00'; $location = 'New Delhi'; $recursive = '1'; ......................... ......................... // other data $recursive=1表示每周,2表示每月 现在我想做的是,如果递归类型是weekly,那么加上7天直到3个月,如果递归类型是monthly,那么加上1个月直到3个月 示例:

我有这样的数据:

$date = '01-01-2014';
$time = '15:20:00';
$location = 'New Delhi';
$recursive = '1';
.........................
.........................  // other data
$recursive=1表示每周,2表示每月

现在我想做的是,如果递归类型是weekly,那么加上7天直到3个月,如果递归类型是monthly,那么加上1个月直到3个月

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

在上面的例子中,意思是说,
$recursive
是每周的,所以要得到1月、2月和3月的递归日期。
因此,预期的结果是:

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)
例2:$date=
15-04-2014
和$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)
01-01-2014 
01-02-2014 (recursiive date in february)
01-03-2014 (recursiive date in march)
例3:$date=
01-01-2014
和$recursive=
2
然后获取4月、5月和6月的递归日期

这是每月递归的,意味着向其中添加1个月

输出:

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)
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
//必须设置默认时区。。所以我刚做了这个,你应该已经在你的.ini文件中了
日期默认时区设置(“美国/洛杉矶”);
$date='01-01-2014';
$time='15:20:00';
$location=‘新德里’;
$recursive='1';
//设置开始日期和结束日期
$startdate=创建日期($date);
$enddate=创建日期($date);
$enddate=date\u add($enddate,date\u interval\u create\u from\u date\u string(“3个月”);
//设置间隔字符串
如果($recursive==“1”){
$str=“7天”;
}否则{
$str=“1个月”;
}
函数recursivefunc($str,$start,$end){
//如果起点等于或大于终点,则弹出。
$s=日期格式($start,“Y/m/d”);
$e=日期格式($end,“Y/m/d”);
if(strotime($s)>=strotime($e)){
返回1;
}
echo date_格式($start,“Y/m/d”),“
”;//打印每个循环的开始日期 $newDate=date_add($start,date_interval_create_from_date_string($str));//增加开始时间 recursiveFunc($str,$newDate,$end);//再次调用该函数 } 递归函数($str,$startdate,$enddate);//对函数的初始调用
我就是这样解决的

$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);
$date='01-01-2014';
$location=‘新德里’;
$start=strottime(“+2个月”,strottime($date))//开始日期
$end_date=日期(“Y-m-t”,$start);//月末最后一天
$end=strottime($end\u日期)//最后日期
/*如果递归类型是weekly*/
如果($json['recurrence']=“1”)
{
对于($dd=$start;$dd$new\u date),
“位置”=>$location
);
$dd=strottime(“+7天,$dd);//添加7天
}
}
如果($json['recurrence']=“2”)
{
对于($dd=$start;$dd$new\u date),
“位置”=>$location
);
$dd=strotime(“+1个月,$dd);//添加1个月
}
}
回声“;
打印(数据);

谢谢你的回答,我会检查一下。