Question

Hey guys, just a simple question.

How do you get the current hour and the nearest hour, e.g.

The time is now 2:25PM.

The current hour is 2:00PM

The nearest hour to be 2:59PM (not 3PM, as 2:59PM will do).

Cheers.

Was it helpful?

Solution

You can use the date function :

$current = date("g:00A");
$next = date("g:59A");

If you want the real time :

$now = date("g:iA");

OTHER TIPS

If I understand correctly:

<?php
$hour = date("g:00A"); // or "G" for 24-hour clock
$nearest_hour = date("g:59A");
?>

You can try something like this:

$hour = date('H');
$min = date('i');

if($min > 30)
{
  $closest_hour = $hour +1;
}
elseif($min < 30)
{
  $closest_hour = $hour;
}
else
{
  //Here is exactly half past $hour so you decide what to do :)
}

HTH!

So you have HH:MM.

Why just keep HH and add 59 next to it ? Isn't that work all the time ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top