D
D
Dealaxer2016-08-22 23:59:48
PHP
Dealaxer, 2016-08-22 23:59:48

PHP How to add two specific DateTime dates?

Welcome all!
I'm looking at DateTime examples, either I'm not looking well, or it's not there, but I can't find a solution for adding two specific time dates, for example: 10:55:00 + 05:35:00.
We have:
The first time value (Separately displayed via ECHO is excellent):
$counttimeback->format("%H:%I:%S")
Second time value (Separately displayed via ECHO is excellent):
$counttimethere->format("%H:%I:%S")
I am trying to summarize in this way:

$fulltime = $counttimeback->format("%H:%I:%S") + $counttimethere->format("%H:%I:%S");
//вывод
echo $fulltime->format("%H:%I:%S");

Nothing comes out.
Can you tell me how to sum the two times?
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dealaxer, 2016-08-23
@Dealaxer

My solution that I needed:

$time = new DateTime('00:00:00');
$fulltime = clone $time;
$time->add($counttimethere);
$time->add($counttimeback);
                      
echo 'Общее время:' . $fulltime->diff($time)->format("%H:%I:%S");

DateTime is a complete gem, why is there no simple + or - ?!

D
D', 2016-08-23
@Denormalization

>Tell me how to sum two times
Algorithm:
1) Calculate the difference in seconds\minutes\years between time
2) Add the difference to the first time
3) ???
4) Profit!

D
donflash, 2016-08-23
@donflash

$d1 = new \DateTime('2016-08-22 10:55:00');
$d1->add(new \DateInterval('PT5H35M'));

var_dump($d1);

object(DateTime)#1 (3) {
["date"]=>
string(26) "2016-08-22 16:30:00.000000"
["timezone_type"]=>
int(3)
["timezone"]= >
string(13) "Europe/Moscow"
}

A
Alexander Shapoval, 2016-08-23
@AlexanderShapoval

And then there's Carbon. But this is the case when "just adding two dates" is not enough
carbon.nesbot.com/docs

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question