0

How can I add time to a digit time value,

currently my code looks like this:

$time = strtotime('00:00:00');

$addTime = strtotime('+5 minutes', $time);
$addTime = date('h:i:s', $addTime);

If I echo $addTime I get this value:

1472680800147268110012:05:00

which is obviously wrong.

It should look like this:

00:05:00
utdev
  • 3,942
  • 8
  • 40
  • 70
  • Here you go https://eval.in/632975 – Noman Sep 01 '16 at 07:56
  • 3
    Possible duplicate of [php - add two hours to date variable](http://stackoverflow.com/questions/7811609/php-add-two-hours-to-date-variable) – dubes Sep 01 '16 at 07:59

3 Answers3

0

Replace $addTime = date('h:i:s', $addTime); with $addTime = date('H:i:s', $addTime);

According to manual http://php.net/manual/en/function.date.php

H 24-hour format of an hour with leading zeros 00 through 23

Andrej
  • 7,474
  • 1
  • 19
  • 21
  • wow.. I think this did it, I will test it a couple times more and if it still works I will give you a tick – utdev Sep 01 '16 at 07:54
0

It should be like this:

<?php
$addMinutes = 16;
$yourTime = new DateTime('08:02:00');
$yourTime->add(new DateInterval('PT' . $addMinutes . 'M'));

$result = $yourTime->format('H:i:s');

var_dump($result);
?>

Demo

Noman
  • 1,459
  • 2
  • 18
  • 38
0

Why don't simply write:

$time = strtotime('00:00:00');

$addTime = date('H:i:s', $time+5*60);
Davide Visentin
  • 735
  • 5
  • 19