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

标签 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); 
 } 

最佳答案

也许是时候开始使用 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/

相关文章:

php - 计算 php 所用时间 - 跨度超过 24 小时

用于获取日期范围内所有星期一的php函数

php, date() 没有返回正确的日期/时间

带日期戳的 PHP 重命名

java - Android - 时间不更新

php strtotime +2.5 小时错误?

php - 从 HTML 页面中删除 MySQL 条目

php - 计算 PHP 字符串中特定字符的所有出现次数的最有效方法是什么?

php - 如何对数组进行排序以便管理顶部的最大字符串长度?

date - 确定所选日期的数量