Given a series of hours, minutes, and seconds [ex: 01:30:00 or 00:30:00], I would like to add up each and convert the sum to seconds.
For example, if the total time is 02:30:00, the total time should be in seconds.
Thanks!
asked May 16, 2011 at 15:19
$timestr = '00:30:00';
$parts = explode[':', $timestr];
$seconds = [$parts[0] * 60 * 60] + [$parts[1] * 60] + $parts[2];
answered May 16, 2011 at 15:22
Marc BMarc B
351k42 gold badges402 silver badges486 bronze badges
1
OK.
basic multiplication:
Demo: //codepad.org/PaXcgdNc
answered May 16, 2011 at 15:22
NaftaliNaftali
143k39 gold badges240 silver badges299 bronze badges
0
Try this:
$string = "1:30:20";
$components = explode[":", $string];
$seconds = $components[0]*60*60 + $components[1]*60 + $components[2]
answered May 16, 2011 at 15:22
Sam MaguraSam Magura
7666 silver badges13 bronze badges
Try
answered May 16, 2011 at 15:23
RobRob
1,2052 gold badges19 silver badges44 bronze badges
The following is really easy way to add days, minutes, hours and seconds to a time using PHP. Using the date function to set the format of the date to be returned then using strtotime to add the increase or decrease of time then after a comma use another strtotime passing in the start date and time.
//set timezone
date_default_timezone_set['GMT'];
//set an date and time to work with
$start = '2014-06-01 14:00:00';
//display the converted time
echo date['Y-m-d H:i',strtotime['+1 hour +20 minutes',strtotime[$start]]];
Times can be entered in a readable way:
- +1 day = adds 1 day
- +1 hour = adds 1 hour
- +10 minutes = adds 10 minutes
- +10 seconds = adds 10 seconds
To sub-tract time its the same except a - is used instead of a +
Home » How To Guides » PHP » How to add Days, Hours, Minutes, and Seconds to Datetime in PHP
Here we’ll provide the simplest way to add days, minutes, hours and seconds to time using PHP. In PHP, using date[]
and strtotime[]
function you can easily increase or decrease time. The provided PHP code lets you do the following works.
- Add days to datetime.
- Add hours to datetime.
- Add minutes to datetime.
- Add seconds to datetime.
$startTime = date["Y-m-d H:i:s"];//display the starting time
echo 'Starting Time: '.$startTime;//add 1 hour to time
$cenvertedTime = date['Y-m-d H:i:s',strtotime['+1 hour',strtotime[$startTime]]];//display the converted time
echo 'Converted Time [added 1 hour]: '.$cenvertedTime;//add 1 hour and 30 minutes to time
$cenvertedTime = date['Y-m-d H:i:s',strtotime['+1 hour +30 minutes',strtotime[$startTime]]];//display the converted time
echo 'Converted Time [added 1 hour & 30 minutes]: '.$cenvertedTime;//add 1 hour, 30 minutes and 45 seconds to time
$cenvertedTime = date['Y-m-d H:i:s',strtotime['+1 hour +30 minutes +45 seconds',strtotime[$startTime]]];//display the converted time
echo 'Converted Time [added 1 hour, 30 minutes & 45 seconds]: '.$cenvertedTime;//add 1 day, 1 hour, 30 minutes and 45 seconds to time
$cenvertedTime = date['Y-m-d H:i:s',strtotime['+1 day +1 hour +30 minutes +45 seconds',strtotime[$startTime]]];//display the converted time
echo 'Converted Time [added 1 day, 1 hour, 30 minutes & 45 seconds]: '.$cenvertedTime;
Using the above code you can add time to current time or any desire time. To sub-track use the same code except -
instead of +
.
How to add hours to time in php?
In your case to increase the current time by 10 hours: $date = date['h:i:s A', strtotime['+10 hours']];
How can I add minutes and minutes in php?
For this, you can use the strtotime[] method. $anyVariableName= strtotime['anyDateValue + X minute']; You can put the integer value in place of X.
How to time add in php?
PHP | DateTime add[] Function The DateTime::add[] function is an inbuilt function in PHP which is used to add an amount of time [days, months, years, hours, minutes and seconds] to the given DateTime object.
How can I get minutes in php?
$min = $interval ->days * 24 * 60; $min += $interval ->h * 60; $min += $interval ->i; // Printing the Result in Minutes format.