Alex
Alex

Reputation: 68416

PHP - adjust time() to be divisible by 5

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

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.

Upvotes: 2

Views: 868

Answers (2)

prabeen giri
prabeen giri

Reputation: 803

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

Upvotes: 2

zerkms
zerkms

Reputation: 254906

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

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

Upvotes: 9

Related Questions