strtotime可以在给定日期之后返回一天的日期和时间吗?

时间:2014-03-20 16:19:48

标签: php date strtotime

我正在使用PHP生成重复日期来处理日期。我有一个$ startDateTime和$ endDateTime。这两个变量是系列中的第一个日期。

如果偶数在每个星期二重复,我需要的东西是

 $repeatDay = "Tuesday";
 $followingDay = strtotime($startDateTime. " following $repeatDay");

使用“next $ repeatDay”不起作用,因为我从今天起没有生成日期。

修改

似乎每五个循环时间在一个小时内向前跳跃。这可能是因为$ start =“2014-04-29 11:00:00”而strtotime只是正确转换日期。

我应该如何将2014-04-29 11:00:00转换为strtotime理解的格式?

 $firstOccurrence=strtotime($start);
 $nextOccurence=strtotime("next $day", $firstOccurrence); //Set the first recurring date for this day
 while($nextOccurence<=strtotime($activeUntil)){
      echo date("M d, Y H:m:i",$nextOccurence)." | ";
      $nextOccurence=strtotime("next $day", $nextOccurence); 
 } 

2 个答案:

答案 0 :(得分:2)

strtotime()可以使用2个参数。第一个是字符串,第二个是基本时间戳。

尝试以下方法:

// Assuming $startDateTime is a timestamp.
$followingDay = strtotime("next $repeatDay", $startDateTime);

答案 1 :(得分:2)

也许是时候开始使用DateTime了?这在日期很复杂。例如,从$ start创建日期时间如下所示:

$start="2014-04-29 11:00:00";
$dateTime=DateTime::createFromFormat("Y-m-d H:m:i", $start);

因为你有$ dateTime,你可以修改一天:

$dateTime->modify('+1 day');
//or just
$dateTime->modify('next tuesday');
//and return it as string
echo $dateTime->format("M d, Y H:m:i");

DateTime了解strtotime所做的一切,因此它可以改善您的解决方案。亲自尝试一下,如果有帮助,请告诉我。