我正在使用 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);
}
最佳答案
也许是时候开始使用 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 所做的一切,因此它可以改进您的解决方案。自己尝试一下,如果有帮助请告诉我。
关于php - strtotime 可以返回给定日期之后一天的日期和时间吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22539052/