Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add time in PHP

Tags:

php

time

How can I add a given number of time in PHP?

Below is are three time variables I would like to add:

time1: "00:02:00"
time2: "00:15:00"
time3: "00:08:00"

This was my approach in attempting to achieve that:

echo date('h:i:s', strtotime('00:02:00') + strtotime('00:15:00') + strtotime('00:08:00'));

The result of my code is 12:25:00, while the expected result should have been 00:25:00.

What am I doing wrong and how can I add given time variables effectively in PHP?

like image 500
Erik Andershed Avatar asked Dec 21 '25 15:12

Erik Andershed


1 Answers

Change from h into H.

h => 12-hour format of an hour with leading zeros

H => 24-hour format of an hour with leading zeros

Try

echo date('H:i:s', strtotime('00:02:00') + strtotime('00:15:00') + strtotime('00:08:00'));

Reference

like image 128
Hassaan Avatar answered Dec 23 '25 06:12

Hassaan