Add time to current time in php

I am trying to add 15minutes to the current time. I am using the following code:

$curtime = date['H:i'];                     
$newtime = $curtime + strtotime['+15 minutes'];

But this still only prints the current time and not the current time + 15.

I want it to add 15mins like so

e.g. If the time is 12:30 the time after addition will be 12:45

Thanks.

peterh

11.1k15 gold badges80 silver badges102 bronze badges

asked Nov 17, 2011 at 1:48

Duncan PalmerDuncan Palmer

2,83710 gold badges60 silver badges90 bronze badges

Close, you want:

$new_time = date['H:i', strtotime['+15 minutes']];

animuson

52.7k28 gold badges139 silver badges145 bronze badges

answered Nov 17, 2011 at 1:52

0

You could do:

echo date['H:i', [time[] + [15 * 60]]];

answered Nov 17, 2011 at 1:51

Josh FoskettJosh Foskett

3,9955 gold badges31 silver badges35 bronze badges

try this:

$curtime = date['H:i'];
$newtime = strtotime[$curtime] + [15 * 60];
echo date['H:i', $newtime];

answered Nov 17, 2011 at 1:51

Book Of ZeusBook Of Zeus

49k18 gold badges174 silver badges169 bronze badges

you can try this - strtotime["+15 minutes"]

answered Nov 17, 2011 at 1:52

Zoltan TothZoltan Toth

46.3k11 gold badges116 silver badges134 bronze badges

In case anyone wants to make it work in Object-oriented way, here is the solution:

$currentTime = new DateTime[];
$currentTime->add[new TimeInterval['PT15M']];
// print the time
echo $currentTime->format['H:i'];

Requires PHP >= 5.3

answered Nov 17, 2011 at 1:58

RaptorRaptor

51.7k43 gold badges220 silver badges356 bronze badges

2

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 time to date PHP?

php $date=strtotime["tomorrow"]; echo date["Y-m-d h:i:sa", $date] . "
"; $date=strtotime["next Sunday"]; echo date["Y-m-d h:i:sa", $date] . "
"; $date=strtotime["+3 Months"]; echo date["Y-m-d h:i:sa", $date] .

How to add hours in PHP?

In your case to increase the current time by 10 hours: $date = date['h:i:s A', strtotime['+10 hours']];

How to get time from DateTime in PHP?

php $date = "30-12-1899 9:25:52 AM"; $date = strtotime[$date]; echo date['H:i:s', $date]; ?>

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'];

Chủ Đề