I would like to calculate with PHP the start and end datetime of an event. I get the start of this event and the duration to add to get the end. So I tried the following code:
$startTime = $this->getStartTime();
$endTime = $this->getStartTime();
$endTime->add(new DateInterval('PT75M'));
in this example I add 75 minutes to the start time and I calculate the end of the event. It works, however it edits also the start time. I read in the PHP docs that the ADD method edits the object which is called on but I don't understand how it could edit the startEdit variable. I don't use reference in any of the methods that I wrote in the example, neither in the getStartTime function
For this, you can use the strtotime() method. $anyVariableName= strtotime('anyDateValue + X minute'); You can put the integer value in place of X.
$min += $interval ->h * 60; $min += $interval ->i; // Printing the Result in Minutes format. echo ( "Difference in minutes is: " );
Example. <? php //Creating a DateTime object $date = date_create("25-09-1989"); //Adding interval to the date $res = date_add($date, new DateInterval('PT10H30S')); //formatting the date to print it $format = date_format( $res, "d-m-Y H:i:s"); print($format); ?>
echo "The time is " . date("h:i:sa"); ?> Note that the PHP date() function will return the current date/time of the server!
You have to create a new DateTime instance for that or you will be editing your original reference to your start date DateTime object. Try something like this:
$endTime = clone $startTime;
$endTime->add(new DateInterval('PT75M'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With