How to add hours and minutes to time in php?

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 +

I have a form that receives a time value:

$selectedTime = $_REQUEST['time'];

The time is in this format - 9:15:00 - which is 9:15am. I then need to add 15 minutes to this and store that in a separate variable but I'm stumped.

I'm trying to use strtotime without success, e.g.:

$endTime = strtotime("+15 minutes",strtotime($selectedTime)));

but that won't parse.

asked Dec 13, 2013 at 0:32

user982124user982124

4,32413 gold badges60 silver badges133 bronze badges

3

Your code doesn't work (parse) because you have an extra ) at the end that causes a Parse Error. Count, you have 2 ( and 3 ). It would work fine if you fix that, but strtotime() returns a timestamp, so to get a human readable time use date().

$selectedTime = "9:15:00";
$endTime = strtotime("+15 minutes", strtotime($selectedTime));
echo date('h:i:s', $endTime);

Get an editor that will syntax highlight and show unmatched parentheses, braces, etc.

To just do straight time without any TZ or DST and add 15 minutes (read zerkms comment):

 $endTime = strtotime($selectedTime) + 900;  //900 = 15 min X 60 sec

Still, the ) is the main issue here.

answered Dec 13, 2013 at 0:51

How to add hours and minutes to time in php?

3

Though you can do this through PHP's time functions, let me introduce you to PHP's DateTime class, which along with it's related classes, really should be in any PHP developer's toolkit.

// note this will set to today's current date since you are not specifying it in your passed parameter. This probably doesn't matter if you are just going to add time to it.
$datetime = DateTime::createFromFormat('g:i:s', $selectedTime);
$datetime->modify('+15 minutes');
echo $datetime->format('g:i:s');

Note that if what you are looking to do is basically provide a 12 or 24 hours clock functionality to which you can add/subtract time and don't actually care about the date, so you want to eliminate possible problems around daylights saving times changes an such I would recommend one of the following formats:

!g:i:s 12-hour format without leading zeroes on hour

!G:i:s 12-hour format with leading zeroes

Note the ! item in format. This would set date component to first day in Linux epoch (1-1-1970)

answered Dec 13, 2013 at 0:42

How to add hours and minutes to time in php?

Mike BrantMike Brant

69.5k10 gold badges97 silver badges102 bronze badges

4

strtotime returns the current timestamp and date is to format timestamp

  $date=strtotime(date("h:i:sa"))+900;//15*60=900 seconds
  $date=date("h:i:sa",$date);

This will add 15 mins to the current time

answered Dec 13, 2018 at 10:24

How to add hours and minutes to time in php?

Nishan BNishan B

4474 silver badges5 bronze badges

To expand on previous answers, a function to do this could work like this (changing the time and interval formats however you like them according to this for function.date, and this for DateInterval):

(I've also written an alternate form of the below function here.)

// Return adjusted time.

function addMinutesToTime( $time, $plusMinutes ) {

    $time = DateTime::createFromFormat( 'g:i:s', $time );
    $time->add( new DateInterval( 'PT' . ( (integer) $plusMinutes ) . 'M' ) );
    $newTime = $time->format( 'g:i:s' );

    return $newTime;
}

$adjustedTime = addMinutesToTime( '9:15:00', 15 );

echo '<h2>Adjusted Time: ' . $adjustedTime . '</h2>' . PHP_EOL . PHP_EOL;

answered Apr 4, 2017 at 20:21

How to add hours and minutes to time in php?

get After 20min time and date

function add_time($time,$plusMinutes){

   $endTime = strtotime("+{$plusMinutes} minutes", strtotime($time));
   return date('h:i:s', $endTime);
}

20 min ago Date and time

date_default_timezone_set("Asia/Kolkata");
echo add_time(date("Y-m-d h:i:sa"),20);

answered Aug 28, 2021 at 20:22

How to add hours and minutes to time in php?

In one line

$date = date('h:i:s',strtotime("+10 minutes", strtotime($date)));

answered Jun 24 at 1:40

RuydoRuydo

3096 silver badges17 bronze badges

You can use below code also.It quite simple.

$selectedTime = "9:15:00";
echo date('h:i:s',strtotime($selectedTime . ' +15 minutes'));

answered Oct 11, 2017 at 13:12

How to add hours and minutes to time in php?

Bhavesh PatelBhavesh Patel

991 gold badge2 silver badges12 bronze badges

Current date and time

$current_date_time = date('Y-m-d H:i:s');

15 min ago Date and time

$newTime = date("Y-m-d H:i:s",strtotime("+15 minutes", strtotime($current_date)));

DaFois

2,1868 gold badges23 silver badges39 bronze badges

answered Jun 7, 2018 at 12:26

How to add hours and minutes to time in php?

Quite easy

$timestring = '09:15:00';
echo date('h:i:s', strtotime($timestring) + (15 * 60));

answered Sep 15, 2020 at 13:28

LightwaxxLightwaxx

4891 gold badge6 silver badges16 bronze badges

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 do you add hours on Strtotime?

You can use DateTime::modify to add time, but I would just do time()+10800 . Show activity on this post. $time = new DateTime("+ 3 hour"); $timestamp = $time->format('Y-M-d h:i:s a');

What does time () do in PHP?

The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

What is timestamp format in PHP?

What is a TimeStamp? A timestamp in PHP is a numeric value in seconds between the current time and value as at 1st January, 1970 00:00:00 Greenwich Mean Time (GMT). The value returned by the time function depends on the default time zone. The default time zone is set in the php.