Domanda

Ho bisogno di arrotondare alcune volte.Mi piacerebbe sapere come posso arrotondare un momento alla prossima ora e usarlo come int in php.

ex:

24:00:02 -> 25  
33:15:05 -> 34  
48:40:30 -> 49
.

Qualche idea?

Saluti Claudio

È stato utile?

Soluzione

Se si gestisce la stringa del tempo come un numero, PHP lo tratta come uno e rimuove tutto ma le prime cifre.

print '48:40:30' + 1; # 49
print '30:00:00' + 1; # 31
.

Altri suggerimenti

Volerai qualcosa come datetime :: sextime .Utilizzare le funzioni della data per estrarre le ore / minuti / secondi, capire se l'ora deve essere incrementata, quindi impostare l'ora sul valore appropriato e azzerare i minuti e secondi.

hmm.Per leggere nuovamente la tua domanda, qualcosa del genere:

$sec = date('s', $timevalue);
$min = date('i', $timevalue);
$hour = date('G', $timevalue);

if (($sec > 0) or ($min > 0)) { $hour++; } // if at x:00:01 or better, "round up" the hour
.

Assumendo il formato che stai utilizzando è ore: minuti: secondi, esprimendo una durata del tempo piuttosto che un periodo di giorno, e suppongo che tu abbia questo valore come una stringa.In tal caso, la tua soluzione è una funzione di casa-birra, in quanto è un caso piuttosto specifico.Qualcosa del genere:

function roundDurationUp($duration_string='') {
     $parts = explode(':', $duration_string);
     // only deal with valid duration strings
     if (count($parts) != 3)
       return 0;

     // round the seconds up to minutes
     if ($parts[2] > 30)
       $parts[1]++;

     // round the minutes up to hours
     if ($parts[1] > 30)
       $parts[0]++;

     return $parts[0];
    }

print roundDurationUp('24:00:02'); // prints 25
print roundDurationUp('33:15:05'); // prints 34
print roundDurationUp('48:40:30'); // prints 49
.

Provalo: http://codepad.org/nu9tljgq

function ReformatHourTime($time_str){
                $min_secs = substr($time_str,3);
                $direction = round($min_secs/60);

                if($dir == 1){
                    return $time_str+1;
                }else{
                    return floor($time_str);
                }
         }
.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top