1

I managed to dynamically load and compare several time values in PHP.

Right now I am stuck here:

$additional_time = $entry_start->diff($compare_from_timeformat);
$additional_time ->format("H:i");
$avaliabletime->modify('+1 hours');

I want to replace the +1 with $avaliabletime but if i try something like this:

$avaliabletime->modify('+'.$additional_time.' hours');

I get this error:

Catchable fatal error: Object of class DateInterval could not be converted to string

So I got 2 questions now.

  1. is there a way to use a variable with the modify part ?
  2. can I also add minutes in the same string ? for example $avaliabletime->modify('+01:45 hours'); ?
John Conde
  • 217,595
  • 99
  • 455
  • 496
Gramrock
  • 49
  • 7
  • In coding `I am done` is not acceptable. If you follow the same mindset , you will not be able to handle **Complex Projects** for sure! Please be optimistic. – Pratik Joshi Apr 16 '15 at 15:42
  • 1
    with I am almost done I meant that I am almost finished, sry, I may have sounded like i gave up, but I didn't. I am very much aware of all the great pro's being active here who knows stuff better than I do :)) – Gramrock Apr 16 '15 at 23:17

1 Answers1

4

$additional_time is a DateInterval object, not a DateTime object or string. To modify your DateTime object by the amount that DateInterval represents use DateTime::add():

$additional_time = $entry_start->diff($compare_from_timeformat);
$avaliabletime->add($additional_time);

If you want to add additional time then you can use DateTime::modify():

$additional_time = $entry_start->diff($compare_from_timeformat);
$avaliabletime->add($additional_time);
$avaliabletime->modify('+45 minutes');
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Thank you John ! No the Minutes are present in $avaliabletime i.e.' 01:30'. So if I use $avaliabletime->add($additional_time); that would also include the minutes right? If so i wouldnt need the -> modify part – Gramrock Apr 17 '15 at 18:45