dongwei4103 2014-03-20 16:19
浏览 202
已采纳

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); 
 } 
  • 写回答

2条回答 默认 最新

  • dsgwii4867 2015-02-23 20:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?