3

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).

peterh
  • 11,875
  • 18
  • 85
  • 108
MacMac
  • 34,294
  • 55
  • 151
  • 222

4 Answers4

5

You can use the date function :

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

If you want the real time :

$now = date("g:iA");
krtek
  • 26,334
  • 5
  • 56
  • 84
2

If I understand correctly:

<?php
$hour = date("g:00A"); // or "G" for 24-hour clock
$nearest_hour = date("g:59A");
?>
Grim...
  • 16,518
  • 7
  • 45
  • 61
1

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!

SubniC
  • 9,807
  • 4
  • 26
  • 33
0

So you have HH:MM.

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

Haza
  • 2,991
  • 1
  • 16
  • 13