3

現在の時間と最も近い時間をどのように取得しますか。

今がその時です2:25PM

現在の時間は2:00PM

最も近い時間です2:59PM(そう3PM2:59PMはありません)。

4

4 に答える 4

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");
于 2011-03-16T14:31:47.930 に答える
2

If I understand correctly:

<?php
$hour = date("g:00A"); // or "G" for 24-hour clock
$nearest_hour = date("g:59A");
?>
于 2011-03-16T14:31:21.533 に答える
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!

于 2011-03-16T14:31:58.347 に答える
0

So you have HH:MM.

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

于 2011-03-16T14:32:11.403 に答える