Calcolo tempo percorso: dove sbaglio??
-
Ciao gente, ho una funzione che mi mette a video il tempo impiegato ad eseguire una determinata operazione.
Ho sempre pensato andasse bene, oggi mi han detto che ha dato alcuni risultati assurdi, ma non mi pare presenti problemi...Ho perciò controllato meglio ed effettivamente in un'operazione iniziata alle "2009-06-16 15:52:22" e terminata alle "2009-06-16 15:53:00", ovvero meno di 1 minuti, la funzione segna 1 minuto e 4"...
Solo che non capisco cosa vada storto...
Riuscite a darci un occhio??
Ho un DB con l'orario di inizio e di fine dell'operazione, questa la dichiarazione:
[FONT=Courier New][COLOR=#0000bb]CREATE TABLE [/COLOR][COLOR=#007700]`[/COLOR][COLOR=#0000bb]tabella[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]` ( [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700] `[/COLOR][COLOR=#0000bb]id_ug[/COLOR][COLOR=#007700]` [/COLOR][COLOR=#0000bb]int[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]10[/COLOR][COLOR=#007700]) [/COLOR][COLOR=#0000bb]NOT NULL auto_increment[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700] `[/COLOR][COLOR=#0000bb]id_u[/COLOR][COLOR=#007700]` [/COLOR][COLOR=#0000bb]int[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]10[/COLOR][COLOR=#007700]) [/COLOR][COLOR=#0000bb]NOT NULL [/COLOR][COLOR=#007700]default [/COLOR][COLOR=#dd0000]'0'[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700] `[/COLOR][COLOR=#0000bb]id_g[/COLOR][COLOR=#007700]` [/COLOR][COLOR=#0000bb]int[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]10[/COLOR][COLOR=#007700]) [/COLOR][COLOR=#0000bb]NOT NULL [/COLOR][COLOR=#007700]default [/COLOR][COLOR=#dd0000]'0'[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700] `[/COLOR][COLOR=#0000bb]active[/COLOR][COLOR=#007700]` [/COLOR][COLOR=#0000bb]int[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]1[/COLOR][COLOR=#007700]) [/COLOR][COLOR=#0000bb]NOT NULL [/COLOR][COLOR=#007700]default [/COLOR][COLOR=#dd0000]'0'[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700] `[/COLOR][COLOR=#0000bb]start[/COLOR][COLOR=#007700]` [/COLOR][COLOR=#0000bb]timestamp NOT NULL [/COLOR][COLOR=#007700]default [/COLOR][COLOR=#0000bb]CURRENT_TIMESTAMP[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700] `[/COLOR][COLOR=#0000bb]end[/COLOR][COLOR=#007700]` [/COLOR][COLOR=#0000bb]timestamp NOT NULL [/COLOR][COLOR=#007700]default [/COLOR][COLOR=#dd0000]'0000-00-00 00:00:00'[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700] `[/COLOR][COLOR=#0000bb]score[/COLOR][COLOR=#007700]` [/COLOR][COLOR=#0000bb]int[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]5[/COLOR][COLOR=#007700]) [/COLOR][COLOR=#0000bb]NOT NULL [/COLOR][COLOR=#007700]default [/COLOR][COLOR=#dd0000]'0'[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]PRIMARY KEY [/COLOR][COLOR=#007700](`[/COLOR][COLOR=#0000bb]id_ug[/COLOR][/FONT][COLOR=#007700][FONT=Courier New]`) [/FONT][/COLOR] [FONT=Courier New][COLOR=#007700])[/COLOR][/FONT]con start ed end i miei valori.
Ho poi la funzione che legge i valori dal DB e li passa a questa funzione:
[FONT=Courier New][COLOR=#007700]function [/COLOR][COLOR=#0000bb]get_date_diff[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$startTime[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]$endTime[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]){ [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$datestartTime [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]date_create[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$startTime[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]); [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$dateendTime [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]date_create[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$endTime[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]); [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$timestampstart [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]strtotime[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$datestartTime[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000bb]format[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"Y-m-d H:i:s"[/COLOR][/FONT][FONT=Courier New][COLOR=#007700])); [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$timestampend [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]strtotime[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000bb]$dateendTime[/COLOR][COLOR=#007700]->[/COLOR][COLOR=#0000bb]format[/COLOR][COLOR=#007700]([/COLOR][COLOR=#dd0000]"Y-m-d H:i:s"[/COLOR][/FONT][FONT=Courier New][COLOR=#007700])); [/COLOR][/FONT] [FONT=Courier New][COLOR=#ff8000]## difference between the two in seconds [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$time_period [/COLOR][COLOR=#007700]= ( [/COLOR][COLOR=#0000bb]$timestampend [/COLOR][COLOR=#007700]- [/COLOR][COLOR=#0000bb]$timestampstart [/COLOR][/FONT][FONT=Courier New][COLOR=#007700]); [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$days [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]0[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$hours [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]0[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$minutes [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]0[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$seconds [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000bb]0[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$time_increments [/COLOR][COLOR=#007700]= array( [/COLOR][COLOR=#dd0000]'g' [/COLOR][COLOR=#007700]=> [/COLOR][COLOR=#0000bb]86400[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT] [FONT=Courier New][COLOR=#dd0000]'h' [/COLOR][COLOR=#007700]=> [/COLOR][COLOR=#0000bb]3600[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT] [FONT=Courier New][COLOR=#dd0000]'m' [/COLOR][COLOR=#007700]=> [/COLOR][COLOR=#0000bb]60[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT] [FONT=Courier New][COLOR=#dd0000]'s' [/COLOR][COLOR=#007700]=> [/COLOR][COLOR=#0000bb]1 [/COLOR][/FONT][FONT=Courier New][COLOR=#007700]); [/COLOR][/FONT] [FONT=Courier New][COLOR=#ff8000]## will hold our values for ( day, minute, hour, seconds ) [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$time_span [/COLOR][/FONT][FONT=Courier New][COLOR=#007700]= array(); [/COLOR][/FONT] [FONT=Courier New][COLOR=#ff8000]## cycle through time_increments [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700]while( list( [/COLOR][COLOR=#0000bb]$key[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]$value [/COLOR][COLOR=#007700]) = [/COLOR][COLOR=#0000bb]each[/COLOR][COLOR=#007700]( [/COLOR][COLOR=#0000bb]$time_increments [/COLOR][/FONT][FONT=Courier New][COLOR=#007700])) { [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$this_value [/COLOR][COLOR=#007700]= (int) ( [/COLOR][COLOR=#0000bb]$time_period [/COLOR][COLOR=#007700]/ [/COLOR][COLOR=#0000bb]$value [/COLOR][/FONT][FONT=Courier New][COLOR=#007700]); [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$time_period [/COLOR][COLOR=#007700]= ( [/COLOR][COLOR=#0000bb]$time_period [/COLOR][COLOR=#007700]% [/COLOR][COLOR=#0000bb]$value [/COLOR][/FONT][FONT=Courier New][COLOR=#007700]); [/COLOR][/FONT] [FONT=Courier New][COLOR=#ff8000]# save value [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$time_span[/COLOR][COLOR=#007700][[/COLOR][COLOR=#0000bb]$key[/COLOR][COLOR=#007700]] = [/COLOR][COLOR=#0000bb]$this_value[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700] } [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$result [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#dd0000]""[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#ff8000]## show results [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700]while( list( [/COLOR][COLOR=#0000bb]$key[/COLOR][COLOR=#007700], [/COLOR][COLOR=#0000bb]$value [/COLOR][COLOR=#007700]) = [/COLOR][COLOR=#0000bb]each[/COLOR][COLOR=#007700]( [/COLOR][COLOR=#0000bb]$time_span [/COLOR][/FONT][FONT=Courier New][COLOR=#007700])) { [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700] if([/COLOR][COLOR=#0000bb]$value [/COLOR][COLOR=#007700]!= [/COLOR][COLOR=#0000bb]0[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]){ [/COLOR][/FONT] [FONT=Courier New][COLOR=#0000bb]$result [/COLOR][COLOR=#007700].= [/COLOR][COLOR=#dd0000]"$value$key "[/COLOR][/FONT][FONT=Courier New][COLOR=#007700]; [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700] } [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700] } [/COLOR][/FONT] [FONT=Courier New][COLOR=#007700] return [/COLOR][COLOR=#0000bb]$result[/COLOR][/FONT][COLOR=#007700][FONT=Courier New]; [/FONT][/COLOR] [FONT=Courier New][COLOR=#007700]}[/COLOR][/FONT]E poi faccio una echo get_data_diff(valore_start, valore_end);
:bho: vedete qualche caso strano in cui potrebbe non funzionare??

Grazie mille!
TeO -
-
Non so perchè la funzione dia errori, sembra formalmente giusta, però secondo me è inutile, daltronde deve fare solo una sottrazione, io la farei direttamente in un unica query:
$sql="Select (unix_timestamp(end) - unix_timestamp(start)) as tempo From tabella";
Ciao! Sembra che tu sia interessato a questa conversazione, ma non hai ancora un account.
Stanco di dover scorrere gli stessi post a ogni visita? Quando registri un account, tornerai sempre esattamente dove eri rimasto e potrai scegliere di essere avvisato delle nuove risposte (tramite email o notifica push). Potrai anche salvare segnalibri e votare i post per mostrare il tuo apprezzamento agli altri membri della comunità.
Con il tuo contributo, questo post potrebbe essere ancora migliore 💗
Registrati Accedi
