-1
$datetime_from = '2013-08-27 14:17:00';
$datetime_till = date("Y-m-d H:i",strtotime("+45 minutes",$datetime_from));

The result is:

$datetime_till = '1970-01-01 01:00:00'

The expected result is

$datetime_till = '2013-08-27 15:02'

How to get it?

Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217

6 Answers6

1

It will be like

$datetime_from = strtotime('2013-08-27 14:17:00');
$datetime_till = date("Y-m-d H:i",strtotime("+45 minutes",$datetime_from));

You need to convert $datetime_from to time

Orelse you can also try like(Iam not sure)

$dateTime = DateTime::createFromFormat('m/d/Y h:i', $datetime_from);
$datetime_from = $dateTime->format('U');
$datetime_till = date("Y-m-d H:i",strtotime("+45 minutes",$datetime_from));
GautamD31
  • 28,552
  • 10
  • 64
  • 85
0

Try-

$datetime_till = date("Y-m-d H:i",strtotime("+45 minutes",strtotime($datetime_from)));
Sahil Mittal
  • 20,697
  • 12
  • 65
  • 90
0

You should use the DateTime class instead of old functional php.

$dateFrom = new \DateTime('2013-08-27 14:17:00');
$dateTill = new \DateTime('2013-08-27 14:17:00');
$dateTill->modify('+45 minutes');

// test if the dates are correct
echo $dateFrom->format('Y-m-d H:i:s'). ' - '.$dateTill->format('Y-m-d H:i:s');

There are numerous other methods you can benefit from the DateTime class.

Laurynas Mališauskas
  • 1,909
  • 1
  • 19
  • 34
0

You can use the DateTime object to do this in procedural style PHP like this:

$datetime_from = date_create('2013-08-27 14:17:00'); // Create a date object from the start date
$datetime_till = date_add($datetime_from, date_interval_create_from_date_string('45 minutes')); // Add the interval to the starting time
echo date_format($datetime_till, 'Y-m-d H:i'); // Format the date how you want it to be output

Hope this helps.

Matt
  • 1,073
  • 1
  • 8
  • 14
0
Use:

$datetime_from = strtotime('2013-08-27 14:17:00');

$datetime_till = date("Y-m-d H:i",$datetime_from+(45*60));
Yatin Trivedi
  • 661
  • 6
  • 9
0

Use DateTime class for date/time modifications :

$datetime_from = new DateTime('2013-08-27 14:17:00');
$datetime_till = clone $datetime_from;
$datetime_till->modify('+45 minutes');

echo
    'From: ' . $datetime_from->format('Y-m-d H:i:s') . "\n".
    'Till: ' . $datetime_till->format('Y-m-d H:i:s');

Output will be :

From: 2013-08-27 14:17:00
Till: 2013-08-27 15:02:00

Valid modify() formats are explained in Date and Time Formats.


Please note that various strtotime() examples are not correct in date/time difference calculation. The simplest example is difference between 2013-03-31 21:00 and 2013-03-30 21:00. Which for naked eye is exact 1 day difference, but if you do subtract this 2 dates, you will get 82800 seconds which is 0.95833333333333 days. This is because of the time change from winter to summer time. DateTime handles leap years and time-zones properly.

Glavić
  • 42,781
  • 13
  • 77
  • 107