• 0

PHP: Add Minutes To Time


Question

This is bugging me now. I can add, say, 20 minutes to the current time using:

<?
$mytime = ($start = date("H:i:s", strtotime( "$start + 20 mins")));
echo $mytime;
}
?>

But...how would one add 20 minutes to a specified time in text format:

<?
FROM $mytime = "18:00:00";
TO $mynewtime = "18:20:00";
?>

Thanks in advance.

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

isnt it just something like...

$var = date("H:is:") + 20000;

:unsure:

edit: nvm that doesnt work :p, ill go search about for u.

edit again: couldnt find anything but u might need to do something with 'strtotime' :unsure:

Edited by soil
Link to comment
Share on other sites

  • 0

use mktime function in php..i cant remember the order of the params, but it wouldbe something like

$timeshift = mktime(yr,mo,day,hr,min+20,seconds);

$val = date("H:i:s",$timeshift);

codes not exactly right, you would have to stick in the yr, mo, etc...but you can do that by just like date('m') for the month (or whateever the month code is) or

Link to comment
Share on other sites

  • 0

<?php
echo date("H:i:s", mktime()+(20*60));
?>

and what user really needs (and didn't post) is this:

$date = date("Y-m-d");//or equivalent
$dateParts = explode("-", $date);
$time = date("H:i:s");//or equivalent
$timeParts = explode(":", $time);
$duration = 20;//min

echo date("H:i:s", mktime($timeParts[0], $timeParts[1], $timeParts[2], $dateParts[1], $dateParts[2], $dateParts[0])+($duration*60));

Link to comment
Share on other sites

  • 0

This thread follows a trend that bugs me. Every solution proposed so far calculates minutes * seconds. Unless the number of minutes you are adding is variable, there is no reason to have PHP do an extra calculation every time the script runs. If you must remind yourself how you go the value, leave a comment or something.

$increment = 1200; //20 min * 60 sec

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.