10

Is there a native way of doing "HH:MM:SS" to seconds with PHP 5.3 rather than doing a split on the colon's and multipling out each section the relevant number to calculate the seconds?


For example in Python you can do :

string time = "00:01:05";
double seconds = TimeSpan.Parse(time).TotalSeconds;

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
benjisail
  • 1,646
  • 5
  • 21
  • 35
  • 2
    The equivalent would be `DateInterval::createFromDateString('00:01:05')->format(arg);` but unfortunately, there is no argument to [`format`](http://de.php.net/manual/en/dateinterval.format.php) that would return the total number of seconds (unlike total number of days). – Gordon Jan 05 '11 at 15:22
  • @Glavić This other question is newer than this one. – benjisail Jan 13 '14 at 08:53

4 Answers4

29

The quick way:

echo strtotime('01:00:00') - strtotime('TODAY'); // 3600
netcoder
  • 66,435
  • 19
  • 125
  • 142
  • yes, the quick way in lines-of-code, but not quick in terms of speed -- strtotime() is a very slow function. – Spudley Jan 05 '11 at 14:30
  • 1
    @Spudley: Actually it's not, compared to a `list`,`explode`,`mktime` combination. Benchmarking it tells me `strtotime` is faster, although who cares, it's micro-optimization. – netcoder Jan 05 '11 at 14:38
  • 1
    I'd like to see some benchmarks on that. `strtotime()` does a lot of work with the string to parse it; I'd be very surprised if it can run quicker than mktime(). (To be honest though, multiplying out as per the original question is still probably the quickest option). – Spudley Jan 05 '11 at 14:52
  • 11
    `echo strtotime('1970-01-01 01:00:00'); // 3600` would be faster than doing both strtotime calls. – Curtis Gibby Aug 16 '11 at 16:53
  • 1
    Just a thought - I suspect the clock isn't frozen between function calls, so if you happened to hit midnight between calls you'd get the wrong answer - you can avoid this by hard-coding a date in for both calls instead of 'TODAY'. – John Carter Jan 02 '14 at 02:22
11

This should do the trick:

list($hours,$mins,$secs) = explode(':',$time);
$seconds = mktime($hours,$mins,$secs) - mktime(0,0,0);
Travesty3
  • 14,351
  • 6
  • 61
  • 98
Spudley
  • 166,037
  • 39
  • 233
  • 307
  • 1
    Hope you don't mind, I edited your answer. If you leave off the optional `$month`, `$day`, and `$year` parameters from `mktime()`, it uses the current month, day, and year. So you need to subtract the seconds since the current date at midnight to get just the seconds from the specified `$time`. – Travesty3 Dec 28 '12 at 17:12
  • @Travesty3 - no problem; editing answers is part of the ethos of this site, so if you see one that needs editing, go right ahead and edit it. – Spudley Dec 28 '12 at 20:12
  • @Travesty3: I think that setting date to 1970-1-1 would be faster, since it is one `mktime` call instead of 2. Use example: `$seconds = mktime($hours,$mins,$secs,1,1,1970);` – Glavić Jan 02 '14 at 01:00
8

I think the easiest method would be to use strtotime() function:

$time = '21:30:10';
$seconds = strtotime("1970-01-01 $time UTC");
echo $seconds;

demo


Function date_parse() can also be used for parsing date and time:

$time = '21:30:10';
$parsed = date_parse($time);
$seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];

demo

Glavić
  • 42,781
  • 13
  • 77
  • 107
1

This function also supports input without hour or minute

$timeInSecs = function ($tym) {
  $tym = array_reverse(explode(':',$tym));
  $hr = $tym[2] ?? 0;
  $min = $tym[1] ?? 0;
  return $hr*60*60 + $min*60 + $tym[0];
};

echo $timeInSecs('1:1:30') . '<br>'; // output: 3690
echo $timeInSecs('1:30') . '<br>'; // output: 90
echo $timeInSecs('30') . '<br>'; // output: 30
proseosoc
  • 1,168
  • 14
  • 23