<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Calcolo tempo percorso: dove sbaglio??]]></title><description><![CDATA[<p dir="auto">Ciao gente, ho una funzione che mi mette a video il tempo impiegato ad eseguire una determinata operazione.<br />
Ho sempre pensato andasse bene, oggi mi han detto che ha dato alcuni risultati assurdi, ma non mi pare presenti problemi...</p>
<p dir="auto">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"...<br />
Solo che non capisco cosa vada storto... <img src="https://dev.connect.gt/assets/plugins/nodebb-plugin-emoji/emoji/customizations/f71a0c43-865f-4388-9868-8b387eebeeb1-mmmm.gif?v=2190ae3dadd" class="not-responsive emoji emoji-customizations emoji--mmm" style="height:1.4em;width:auto;vertical-align:middle" title=":?" alt="" /></p>
<p dir="auto">Riuscite a darci un occhio??</p>
<p dir="auto">Ho un DB con l'orario di inizio e di fine dell'operazione, questa la dichiarazione:</p>
<pre><code>[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]
</code></pre>
<p dir="auto">con start ed end i miei valori.</p>
<p dir="auto">Ho poi la funzione che legge i valori dal DB e li passa a questa funzione:</p>
<pre><code>[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]-&gt;[/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]-&gt;[/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]=&gt; [/COLOR][COLOR=#0000bb]86400[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT]
[FONT=Courier New][COLOR=#dd0000]'h' [/COLOR][COLOR=#007700]=&gt; [/COLOR][COLOR=#0000bb]3600[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT]
[FONT=Courier New][COLOR=#dd0000]'m' [/COLOR][COLOR=#007700]=&gt; [/COLOR][COLOR=#0000bb]60[/COLOR][/FONT][FONT=Courier New][COLOR=#007700], [/COLOR][/FONT]
[FONT=Courier New][COLOR=#dd0000]'s' [/COLOR][COLOR=#007700]=&gt; [/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]
</code></pre>
<p dir="auto">E poi faccio una echo get_data_diff(valore_start, valore_end);</p>
<p dir="auto">:bho: vedete qualche caso strano in cui potrebbe non funzionare?? <img src="https://dev.connect.gt/assets/plugins/nodebb-plugin-emoji/emoji/customizations/f71a0c43-865f-4388-9868-8b387eebeeb1-mmmm.gif?v=2190ae3dadd" class="not-responsive emoji emoji-customizations emoji--mmm" style="height:1.4em;width:auto;vertical-align:middle" title=":?" alt="" /></p>
<p dir="auto">Grazie mille!<br />
TeO</p>
]]></description><link>https://dev.connect.gt/topic/103820/calcolo-tempo-percorso-dove-sbaglio</link><generator>RSS for Node</generator><lastBuildDate>Fri, 27 Nov 2026 21:56:33 GMT</lastBuildDate><atom:link href="https://dev.connect.gt/topic/103820.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 16 Jun 2009 13:57:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Calcolo tempo percorso: dove sbaglio?? on Tue, 16 Jun 2009 19:44:25 GMT]]></title><description><![CDATA[<p dir="auto">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:</p>
<pre><code>$sql="Select (unix_timestamp(end) - unix_timestamp(start)) as tempo From
tabella";
</code></pre>
<p dir="auto"><img src="https://dev.connect.gt/assets/plugins/nodebb-plugin-emoji/emoji/customizations/54a3bffb-5e4a-4eb8-81dc-99b2072e5d05-175.gif?v=2190ae3dadd" class="not-responsive emoji emoji-customizations emoji--ciauz" style="height:1.4em;width:auto;vertical-align:middle" title=":ciauz:" alt="" /></p>
]]></description><link>https://dev.connect.gt/post/838154</link><guid isPermaLink="true">https://dev.connect.gt/post/838154</guid><dc:creator><![CDATA[las]]></dc:creator><pubDate>Tue, 16 Jun 2009 19:44:25 GMT</pubDate></item><item><title><![CDATA[Reply to Calcolo tempo percorso: dove sbaglio?? on Tue, 16 Jun 2009 16:51:36 GMT]]></title><description><![CDATA[<p dir="auto">Non so se è un'alternativa praticabile, ma perchè non usi un campo INT per 'start' e 'end', ci metti dentro la timestamp UNIX (cioè usando <a href="http://php.net/time" rel="nofollow ugc">time()</a>) e non usi quella per calcolare le differenze in secondi?</p>
<p dir="auto">HTH, ciao! <img src="https://dev.connect.gt/assets/plugins/nodebb-plugin-emoji/emoji/android/1f642.png?v=2190ae3dadd" class="not-responsive emoji emoji-android emoji--slightly_smiling_face" style="height:1.4em;width:auto;vertical-align:middle" title=":)" alt="🙂" /></p>
]]></description><link>https://dev.connect.gt/post/838149</link><guid isPermaLink="true">https://dev.connect.gt/post/838149</guid><dc:creator><![CDATA[seo.italy]]></dc:creator><pubDate>Tue, 16 Jun 2009 16:51:36 GMT</pubDate></item></channel></rss>