Reputation: 825
I have a column named hours_worked
in my database the format is Time
(00:00:00
). When I perform time math with PHP it is converted to integer.
So when I print this:
<?=$deposit26['hours_worked'];?>
The output is 30:30:15
which is great.
But when I try performing Time math with another query:
<?=$total_hours_worked = $deposit26['hours_worked'] + $deposit262['hours_worked'];?>
The output becomes 60
. I would like to keep the original Time
format. How is this possible?
Upvotes: 0
Views: 36
Reputation: 108510
Use PHP DateInterval class. http://www.php.net/manual/en/class.dateinterval.php
30:30:15 would be represented ISO 8601 PT30H30M15S
.
$t1 = new DateInterval('PT30H30M15S');
To add another DateInterval to that...
$t2 = new DateInterval('PT05H55M55S');
we can grind through and do the addition. seconds, minutes and hours...
// add seconds, carry minutes
$t1->s += $t2->s ;
$t1->i += (int)( $t1->s / 60 ) ;
$t1->s = $t1->s % 60 ;
// add minutes, carry hours
$t1->i += $t2->i ;
$t1->h += (int)( $t1->i / 60 ) ;
$t1->i = $t1->i % 60 ;
// add hours
$t1->h += t2->h ;
// format as string
echo $t1->format('%h:%I:%S') ;
Or, make use of class that extends DateIterval, such as the one provided by glavic at gmail dot com in the comment section of the PHP documentation http://www.php.net/manual/en/class.dateinterval.php
Upvotes: 0
Reputation: 183
probably because your $total_hours_worked
is being assumed as an integer.
try <?=$deposit26['hours_worked'] += $deposit262['hours_worked'];?>
Upvotes: 1