Asked  1 Year ago    Answers:  5   Viewed   9 times

I would like to convert time strings (Such as 2:12:0) to decimal format in hours (ex 2:12:0 would be 2.2 hours) in PHP.

 Answers

5

A fairly dumb conversion from the top of my head, using explode by colon:

<?php 

$hms = "2:12:0";
$decimalHours = decimalHours($hms);

function decimalHours($time)
{
    $hms = explode(":", $time);
    return ($hms[0] + ($hms[1]/60) + ($hms[2]/3600));
}

echo $decimalHours;

?>
Thursday, April 1, 2021
 
EnTee
 
3

Try

SELECT TIME_FORMAT('10:20:00', '%h:%i %p')

Here is the SQLFiddel Demo

Saturday, May 29, 2021
 
2

How about this?

>>> s = '23.45678'
>>> int(float(s))
23

Or...

>>> int(Decimal(s))
23

Or...

>>> int(s.split('.')[0])
23

I doubt it's going to get much simpler than that, I'm afraid. Just accept it and move on.

Sunday, June 13, 2021
 
smiggle
 
2

The easiest way is to use an NSScanner, specifically the methods scanHexInt: or scanHexLongLong:. Another possibility is to get the C string from the NSString and use C-style functions such as strtol (with base 16).

Monday, August 30, 2021
2
$d = new DateTime('Wed Feb 01 2012 05:00:00 GMT-080');
echo $d->format('Y-m-dTH:i:s');
Thursday, September 2, 2021
 
HamidR
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :