开发者

PHP - adjust time() to be divisible by 5

I'm using PHP's time() to set two dates (default values for two input fields):

  • a start date, which should be the current time: date('m/d/Y H:i', time());
  • a end date, which should be the current time + 2 hours: date('m开发者_运维知识库/d/Y H:i', time() + 60*60*2);

How can I adjust both dates, so the minutes divide with 5?

For example, if the current time is 12/12/2012 14:16, I want to adjust it to 14:20. Or if the current time is 12/12/2012 04:59, I want to adjust it to 05:00.


$time = ceil(time() / 300) * 300;

echo date('m/d/Y H:i', $time);
echo date('m/d/Y H:i', $time + 60 * 60 * 2);


There might be other inbuilt function to achieve you problem , but here is the my solution to your problem

<?php

    $minute = date('i'); 
    $divident = ceil($minute/5); 

    $new_minute = $divident  * 5; 
    $difference = $new_minute - $minute ; 
    date('m/d/Y H:i' ,time() + 60 * $difference   ); // first date 

    date('m/d/Y H:i' ,time() +  $difference * 60 +  (2 * 60 * 60)   ) // second date.
?>

I hope this helps :)

Prabeen

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜