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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上