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

问题描述:

I am working on generating recurring dates using PHP to process the dates. I have a $startDateTime and $endDateTime. These two variables are for the first date in the series.

If the even repeats every Tuesday I need something along the lines of

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

Using "next $repeatDay" doesn't work since I am not generating the date from todays date.

EDIT:

It seems that every five loops the time jumps forward an hour in the date. This may be because $start="2014-04-29 11:00:00" and strtotime is only converting the date correctly.

How should I convert 2014-04-29 11:00:00 to a format that strtotime understands?

 $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); 
 } 

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

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

   $ repeatDay =“Tuesday”; 
 $ followingDay = strtotime($ startDateTime。“在$ repeatDay之后”); 
  code>  pre> 
 
 

使用“next $ repeatDay”不会 工作,因为我没有从今天的日期生成日期。 p>

编辑: strong> p>

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

我该如何转换2014-04-29 11: 00:00到strtotime理解的格式? p>

  $ firstOccurrence = strtotime($ start); 
 $ nextOccurence = strtotime(“next $ day”,$ firstOccurrence);  //设置当天的第一个重复日期
 while($ nextOccurence&lt; = strtotime($ activeUntil)){
 echo日期(“M d,YH:m:i”,$ nextOccurence)。“|”; \  n $ nextOccurence = strtotime(“next $ day”,$ nextOccurence);  
} 
  code>  pre> 
  div>

Maybe it's time to start working with DateTime? It's pretty complex in modyfing dates. For example, creating date time from your $start would look like this:

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

And as you have $dateTime, you can modify it by one day:

$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 understands everything that strtotime does, so it can improve your solution. Try it out yourself, and let me know if this helps.

strtotime() can take 2 parameters. The first is the string and the second is the base timestamp.

Try the following:

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