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条)

报告相同问题?

悬赏问题

  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 spring后端vue前端
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题