• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

  • 0
Sign in to follow this  

PHP: Add Minutes To Time

Question

DjmUK    0

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.

Share this post


Link to post
Share on other sites

7 answers to this question

Recommended Posts

  • 0
soil    1

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
  • Like 1

Share this post


Link to post
Share on other sites
  • 0
SkyyPunk    39

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

Share this post


Link to post
Share on other sites
  • 0
DjmUK    0

I modified mktime() and it works now :D

Thanks guys.

Share this post


Link to post
Share on other sites
  • 0
Adrian    0
<?php
echo date("H:i:s", mktime()+(20*60));
?>

Share this post


Link to post
Share on other sites
  • 0
dimitris    1

<?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));

Share this post


Link to post
Share on other sites
  • 0
cold12    0

Just in case you want a solution that doesn't suck:

$From = "18:00:00";
$Minutes = 20;

$To = date("H:i:s", strtotime($From)+($Minutes*60));

Share this post


Link to post
Share on other sites
  • 0
Hot    226

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.