It is necessary to calculate the time to the nearest necessary hour, the period - every 6 hours, it is 00, 6, 12, 18. Give the time to this hour in seconds. Stopped at the very end.

function itime() { //Текущее время $nt = date('G:i:s', time()); list($hour, $min, $sec) = explode(':', $nt); //Узнаём ближайший час if ($hour >= 0 && $hour < 6) { $wayth = 6; } if ($hour >= 6 && $hour < 12) { $wayth = 12; } if ($hour >= 12 && $hour < 18) { $wayth = 18; } if ($hour >= 18 && $hour < 0) { $wayth = 0; } //Считаем время до этого часа } echo itime(); 
  • one
    Isn't it easier not to leave the seconds at all? And use date ('U') - Artem Gorlachev
  • @Naumov how to get the next hour in unix time? - Rammsteinik
  • For example, so strtotime('6 hours') - Naumov
  • Round by s * 60 * 60 * 6 try - Artem Gorlachev
  • @Naumov strtotime ('6 hours') will not give time with min. and sec. 00:00 - Rammsteinik

3 answers 3

 function itime() { //Текущее время $currentTime = time(); $nt = date('Y:m:d:G:i:s:', $currentTime); list($year,$month,$day, $hour, $min, $sec) = explode(':', $nt); //Узнаём ближайший час if ($hour >= 0 && $hour < 6) { $wayth = 6; } if ($hour >= 6 && $hour < 12) { $wayth = 12; } if ($hour >= 12 && $hour < 18) { $wayth = 18; } if ($hour >= 18 && $hour <= 23){ // новый день $day += 1; //проверяем месяца по 31 дню if ($day == 32){ $month += 1; //проверяем декабрь if ($month == 12){ $year += 1; } } //проверяем месяца по 30 дней elseif ($day == 31 ){ $monthList = array(4,6,9,11); if (in_array($month, $monthList)){ $month += 1; } } //проверяем високосный февраль elseif ($day == 30 && $year%4 == 0 && $month == 2){ $month += 1; } //проверяем просто февраль elseif ($day == 29 && $month == 2){ $month += 1; } $wayth = 0; } $targetTime = mktime($wayth,0,0,$month,$day,$year); //Считаем время до этого часа $result = $targetTime - $currentTime ; return $result; } echo itime(); 
  • Looks like what you need, commented out echo date('Y:m:d:G:i:s:', $targetTime); and as a result reversed - $result = $targetTime-$currentTime; and the result with a minus issued - Rammsteinik
  • Well, yes, exactly, at the end of the working day I am in a stupor
  • And if ($hour >= 18 && $hour >= 0){ instead of if ($hour >= 18 && $hour < 0){ , and then 20, for example, is not in the range - Rammsteinik
 function itime() { $period = 60*60*6; $periods = floor(date('U')/$period); $left = date('U') - $periods*$period; return $left; } echo itime(); 

    so you do not understand and insult is not worth everything just

     if (time() >= strtotime('24 hours') && $hour < strtotime('6 hours')) { $result = strtotime('6 hours') - time(); } 

    learn php.

    • so who does not have the courage to write the reason for the minus? - Naumov