false Posted January 5, 2012 Report Share Posted January 5, 2012 (edited) DB id | dep_time | dep_date | arr_time | arr_date 1 | 18:00 | 02.01.2012 | 02:42 | 03.01.2012 2 | 15:00 | 03.01.2012 | 21:27 | 03.01.2012 3 | 16:25 | 04.01.2012 | 16:41 | 04.01.2012 .. kods kas dabū cik stundas, minūtes pagāja no dep_time līdz arr_time while($flight = mysql_fetch_array($flightsql)){ $date = $flight['dep_date']; $dateA = $flight['arr_date']; $beggin_hour = $flight['dep_time']; $end_hour = $flight['arr_time']; define("SECONDS_PER_HOUR", 60*60); $start = strtotime($date." ".$beggin_hour); $stop = strtotime($dateA." ".$end_hour); $difference = $stop - $start; $hours = round($difference / SECONDS_PER_HOUR, 0, PHP_ROUND_HALF_DOWN); $minutes = ($difference % SECONDS_PER_HOUR) / 60; echo $hours. " " .$minutes. "<br/>"; } iznākums id | stundas | minutes 1 | 8 | 42 2 | 6 | 27 3 | 0 | 36 un kāds var paskaidrot, kā dabūt kopējo stundu, minūšu skaitu? Ir jasanāk 15h 45min. Edited January 5, 2012 by false Quote Link to comment Share on other sites More sharing options...
daGrevis Posted January 5, 2012 Report Share Posted January 5, 2012 Kāpēc ir divas kolonas gan ielidošanas, gan izlidošanas laikiem (tjip, katram pa divām)? To visu varētu glabāt par divām kolonām mazāka izmēra shēmā. Quote Link to comment Share on other sites More sharing options...
false Posted January 5, 2012 Author Report Share Posted January 5, 2012 (edited) Var, bet šādi arī var glabāt, ne? Un lūgums nenovērsties no jautājuma.. Edited January 5, 2012 by false Quote Link to comment Share on other sites More sharing options...
marrtins Posted January 5, 2012 Report Share Posted January 5, 2012 (edited) 1) Tu nepareizi rēķini: $hours = floor($difference / (60 * 60)); $mins = floor(($difference / 60) % 60); $secs = $difference % 60; 2) kopsummas var dabūt summējot. $hoursTotal += $hours; utt. 3) kopsummas var dabūt summējot. { .. $differenceTotal += $difference; .. } $hoursTotal = floor($differenceTotal / (60 * 60)); $minsTotal = floor(($differenceTotal / 60) % 60); $secsTotal = $differenceTotal % 60; Edited January 5, 2012 by marrtins Quote Link to comment Share on other sites More sharing options...
Val Posted January 5, 2012 Report Share Posted January 5, 2012 konstante ciklā, ā jē :) Quote Link to comment Share on other sites More sharing options...
Morphius Posted January 6, 2012 Report Share Posted January 6, 2012 A kāpēc nevar glabāt visus laikus timestamp formāta? Vienkārši pieskaiti vai atņem noteiktu daudzumu sekundes un viss, un tad vajadzības gadījumā kovertē lai būtu lasāmāk?! date("Y-m-d", $timestamp); Quote Link to comment Share on other sites More sharing options...
marrtins Posted January 6, 2012 Report Share Posted January 6, 2012 date("Y-m-d", 3); <- LOL Quote Link to comment Share on other sites More sharing options...
Gints Plivna Posted January 6, 2012 Report Share Posted January 6, 2012 Visu vai gandrīz visu var izdarīt SQLā, PHP ir lieks (tas tā fleimam ;) ) Paspēlējos ar MySQL datumu f-jām BTW sākotnējā uzdevumā dati nesaskan ar rezultātu 3 rindiņā, bet tas sīkums. Tātad: mysql> create table t (id int, dep_time time, dep_date date, arr_time time, arr_ date date); Query OK, 0 rows affected (0.03 sec) mysql> insert into t values (1, '18:00', '2012.01.02', '02:42', '2012.01.03'); Query OK, 1 row affected (0.00 sec) mysql> insert into t values (1, '15:00', '2012.01.03', '21:27', '2012.01.03'); Query OK, 1 row affected (0.00 sec) mysql> insert into t values (1, '16:25', '2012.01.04', '16:41', '2012.01.04'); Query OK, 1 row affected (0.00 sec) mysql> select * from t; +------+----------+------------+----------+------------+ | id | dep_time | dep_date | arr_time | arr_date | +------+----------+------------+----------+------------+ | 1 | 18:00:00 | 2012-01-02 | 02:42:00 | 2012-01-03 | | 1 | 15:00:00 | 2012-01-03 | 21:27:00 | 2012-01-03 | | 1 | 16:25:00 | 2012-01-04 | 16:41:00 | 2012-01-04 | +------+----------+------------+----------+------------+ mysql> SELECT SUM(HOUR(diff)) + FLOOR(SUM(MINUTE(diff))/60) as h, -> MOD(SUM(MINUTE(diff)),60) as m -> FROM ( -> SELECT timediff( -> date_add(arr_date, INTERVAL arr_time HOUR_SECOND), -> date_add(dep_date, INTERVAL dep_time HOUR_SECOND)) AS diff -> FROM t) AS t1; +------+------+ | h | m | +------+------+ | 15 | 25 | +------+------+ 1 row in set (0.00 sec) Gints Plivna http://datubazes.wordpress.com Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.