Jump to content
php.lv forumi

Saskaitīt stundas, minūtes.


false

Recommended Posts

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 by false
Link to comment
Share on other sites

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 by marrtins
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...