duanpanhuo0618 2013-11-26 20:48
浏览 36
已采纳

获取strtotime以在给定时间找到下一个时间戳

I'm trying to make strtotime give me a timestamp, but I only know that it's a hour and minute and it's the next in the row. Fx.

If the given hour and minute is 01:00 (AM) I require it to return the timestamp of 01:00 (AM), if it is the day before at 19:00, I would like it to return the timestamp of 01:00 the next day, but if it is after midnight at 00:30, it should return the timestamp 01:00 the same date.

To simplify it, it's the NEXT occurrence of a time.

Strtotime is giving me the time on the same day, if and not the next occurrence.

  • 写回答

1条回答 默认 最新

  • dtgta48604 2013-11-26 20:50
    关注

    You can easily generate a timestamp, see if it's already past, and then increment the day.

    Something like this perhaps?

    $timestamp = strtotime("1:00 am");
    if($timestamp < time()) {
        $timestamp = strtotime("+1 day",$timestamp);
    }
    

    Or if you want to tighten it up to a one-liner:

    $string = "1:00 am";
    $timestamp = (strtotime($string) > time()) ? strtotime($string) : strtotime("tomorrow " . $string);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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