1

这就是我所拥有的:

Start Date (yyyy-mm-dd)  
Interval : n   
Interval Type : hour, day, week, month, year 

这就是我需要的:是循环间隔中的当前时间/日期

Data/Example:  
Today : 2013-07-16 12:37  

Event Data :  
Start Date : 2012-04-03 12:30  
Interval : 2  
Interval Type : week  

2012-04-03 是星期二。2013-07-16 也是如此。如果每隔一个星期二的重复将达到今天的日期或上一个星期二,如何计算(在 PHP 中)?

任何人?

编辑:
@hek2mgl 我让它这样工作

/* Check for recurring window is legit */
$interval_string ="P".$mm["mm_reoc_interval"];
switch ($mm["mm_reoc_interval_type"])
{
    case 1: $interval_string .= "H"; break;
    case 2: $interval_string .= "D"; break;
    case 3: $interval_string .= "W"; break;
    case 4: $interval_string .= "M"; break;
    case 5: $interval_string .= "Y"; break;
}

$start = DateTime::createFromFormat("Y-m-d H:i:s", $mm["mm_reoc_start_date"]." ".$mm["mm_reoc_start_hour"].":00");
$end = DateTime::createFromFormat("Y-m-d H:i:s", $date_time);
$interval = new DateInterval($interval_string);
$occurrences = 3;
$period = new DatePeriod($start,$interval,$end);
foreach($period as $dt){
  $date_array[] = $dt->format("Y-m-d");
}

if(in_array($today, $date_array)) 
{
       bla.bla..
    }
4

4 回答 4

1

这应该可以解决问题:

<?php
$start = "2012-04-03";
$compare = "2013-07-17";

$start_date = new Datetime($start); 
$compare_date = new Datetime($compare);
$diff = $start_date->diff($compare_date);
$diff_d = ($diff->days);

$round = (int)($diff_d/14); // 14: every 2nd week!
$diff_d2 = $round * 14;

if (0==$diff_d-$diff_d2) {
   echo "<br>Today is the day!";
} else {
   $d1 = $start_date->add(new DateInterval ("P" . $diff_d2 . "D"));
   echo "Last match was " . $d1->format('Y-m-d');
}
?>
于 2013-07-16T11:25:54.007 回答
0

您可以使用strtotime

你可以说像strtotime("+1 week"); strtotime("+1 month");

要相对于当前时间以外的特定时间工作,请将unix epoch time值传递给strtotime

于 2013-07-16T10:45:20.053 回答
0

您可以将该DateTime类与相对日期时间格式一起使用:

$dt = new DateTime('2012-04-03 12:30 +2 weeks');
$now = new DateTime();

if($dt->format('Y-m-d') === $now->format('Y-m-d')) {
    echo "it's today";
}

查看本手册以查看可以在相对日期时间格式中使用的标识符。

于 2013-07-16T10:47:56.077 回答
0

观看DatePeriod类(和DateInterval)。

我希望这个例子特别有用。

于 2013-07-16T11:44:10.120 回答