PHP time format seconds conversion

Converting HH:MM:SS Time Format to Seconds in PHP

This article introduces how to convert HH:MM:SS time format to seconds in the PHP programming language.

Shou Arisaka
1 min read
Oct 20, 2025

This article introduces how to convert HH:MM:SS time format to seconds in the PHP programming language.

Image

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

php - Convert time in HH:MM:SS format to seconds only? - Stack Overflow

I wrote a simple bash utility.


hoursToSeconds(){

  : e.g. hoursToSeconds 2:30:00

cat - <<'EOT' | sed -Ee "s/@ARG1/${1:-"1:00:00"}/g; " > $TMPDIR/tmp.php

<?php

$time = '@ARG1';
$parsed = date_parse($time);
$seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
echo $seconds ;

EOT

trim "$( php -ef $TMPDIR/tmp.php )"

}

Image

$ hoursToSeconds 2:34
9240$ hoursToSeconds 2:34:00
9240$ hoursToSeconds 12:34:00
45240$ hoursToSeconds 1
0$ hoursToSeconds 2:00
7200$

Share this article

Shou Arisaka Oct 20, 2025

๐Ÿ”— Copy Links