34

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.

user982124
  • 4,416
  • 16
  • 65
  • 140

9 Answers9

84

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.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
  • The same issue as in another answer - it will fail on summer time clocks shift if some time near shift is passed – zerkms Dec 13 '13 at 00:52
  • @zerkms: That's the reason to use `strtotime()` as it takes DST into account, unlike addition. Most times you want to take DST into account. Depends on the requirement. – AbraCadaver Dec 13 '13 at 00:55
  • 1
    you cannot take `DST` into account for time only. Time string is not a date, so you cannot tell if in this case DST needs to be applied or not. – zerkms Dec 13 '13 at 00:55
10

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 24-hour format without leading zeroes

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

!H:i:s 24-hour format with leading zeroes

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

munomono
  • 1,243
  • 9
  • 19
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • "This probably doesn't matter if you are just going to add time to it" --- what about summer time? – zerkms Dec 13 '13 at 00:43
  • @zerkms I guess how this is handled really depends on OP needs. If you are just strictly looking for a 12 hour time adding functionality (i.e. you don't want to account for daylight saving changes), you could make the format `!g:i:s` to always set the date to epoch time (1-1-1970) so you don't have such a concern. – Mike Brant Dec 13 '13 at 00:48
  • "If you are just strictly looking" --- not me, but OP is looking for something that will correctly add 15 minutes to a particular time string. Your *currently* provided solution may fail (see ideone link) – zerkms Dec 13 '13 at 00:49
  • @zerkms What I am saying is that may not be considered a failure by OP. It really isn't addressed. i will update answer to reflect `!g:i:s` solution given in comments. – Mike Brant Dec 13 '13 at 01:03
5

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

Nishan B
  • 627
  • 7
  • 11
3

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 '<h1>Adjusted Time: ' . $adjustedTime . '</h1>' . PHP_EOL . PHP_EOL;
Community
  • 1
  • 1
FirstFraktal
  • 358
  • 4
  • 6
0

You can use below code also.It quite simple.

$selectedTime = "9:15:00";
echo date('h:i:s',strtotime($selectedTime . ' +15 minutes'));
Bhavesh Patel
  • 115
  • 1
  • 2
  • 12
0

Quite easy

$timestring = '09:15:00';
echo date('h:i:s', strtotime($timestring) + (15 * 60));
Lightwaxx
  • 625
  • 1
  • 8
  • 18
0

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

In one line

$date = date('h:i:s',strtotime("+10 minutes"));
Ruydo
  • 305
  • 8
  • 18
-1

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,197
  • 8
  • 26
  • 43
Khan arman
  • 11
  • 2