Convert Time in Hh:Mm:Ss Format to Seconds Only

Convert time in HH:MM:SS format to seconds only?

No need to explode anything:

$str_time = "23:12:95";

$str_time = preg_replace("/^([\d]{1,2})\:([\d]{2})$/", "00:$1:$2", $str_time);

sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);

$time_seconds = $hours * 3600 + $minutes * 60 + $seconds;

And if you don't want to use regular expressions:

$str_time = "2:50";

sscanf($str_time, "%d:%d:%d", $hours, $minutes, $seconds);

$time_seconds = isset($seconds) ? $hours * 3600 + $minutes * 60 + $seconds : $hours * 60 + $minutes;

Convert HH:MM:SS string to seconds only in javascript

Try this:

var hms = '02:04:33';   // your input string
var a = hms.split(':'); // split it at the colons

// minutes are worth 60 seconds. Hours are worth 60 minutes.
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);

console.log(seconds);

How to convert a HH:MM:SS string to seconds with PHP?

The quick way:

echo strtotime('01:00:00') - strtotime('TODAY'); // 3600

How to convert an H:MM:SS time string to seconds in Python?


def get_sec(time_str):
"""Get seconds from time."""
h, m, s = time_str.split(':')
return int(h) * 3600 + int(m) * 60 + int(s)


print(get_sec('1:23:45'))
print(get_sec('0:04:15'))
print(get_sec('0:00:25'))

How to turn HH:MM:SS:MS into just seconds?

Use this:

$res = "00:04:12.44";
$ex = explode(".", $res);
$res = $ex[0];
$secs = strtotime("01.01.1970 " . $res);

Or - see the comment of Sven:

$res = "00:03:50.76";
$ex = explode(".", $res);
$res = $ex[0];
$ex = explode(":", $res);
$secs = $ex[0] * 3600 + $ex[1] * 60 + $ex[2]; // 230 secs

Convert a time format to seconds

Try the below code

var hms = '09:05:03';var s = hms.split(':');
var seconds = (+s[0]) * 60 * 60 + (+s[1]) * 60 + (+s[2]); console.log(seconds);var hm = '00:55:03';var s = hm.split(':');
var seconds = (+s[0]) * 60 * 60 + (+s[1]) * 60 + (+s[2]);
console.log(seconds);

How to Convert Hours, minutes and seconds (HH:mm:ss) to minutes and seconds (mm:ss) in SQL

Perhaps a lighter approach with less string manipulation

Example

Declare @T time = '02:47:10'

Select concat(datediff(MINUTE,0,@T),':',datepart(second,@T))

Results

167:10

Convert HH:MM:SS to seconds in momentjs

Try something like this:

moment('12:10:12: PM', 'HH:mm:ss: A').diff(moment().startOf('day'), 'seconds');

returns 43812



Related Topics



Leave a reply



Submit