PHP round the time to the nearest 15 seconds

后端 未结 3 1756
遥遥无期
遥遥无期 2021-01-21 03:32

This is not a duplicate question, but involves a little understanding about time.

I need a solution to the following problem I have a number of specifically produced tim

3条回答
  •  被撕碎了的回忆
    2021-01-21 04:26

    You're massively overcomplicating this, just do rounding on the Unix timestamp level:

    function roundMyTime($time)
    {
      $time = strtotime($time);
      $time = 15*round($time/15);
      echo date('H:i:s', $time)."\n";
    }
    roundMyTime('18:35:17');
    roundMyTime('18:35:27');
    roundMyTime('18:35:37');
    roundMyTime('18:35:47');
    roundMyTime('18:35:57');
    roundMyTime('18:36:07');
    roundMyTime('18:36:17');
    

    Outputs:

    18:35:15
    18:35:30
    18:35:30
    18:35:45
    18:36:00
    18:36:00
    18:36:15
    

    Demo here.

提交回复
热议问题