Z
Z
Zhenya T2014-11-12 12:56:44
PHP
Zhenya T, 2014-11-12 12:56:44

Is there a method like DateTime->add() that doesn't change the DateTime itself?

Actually the essence is in the catch - is there a method that adds a DateInterval, BUT not changing the object from which we called this method, but returning the changed value? Or are we still doomed to use clone, write it to a variable, and only then call add?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Sydorenko, 2014-11-12
@Turowskiy

Yes, if you use the DateTimeImmutable class instead of DateTime. php.net/manual/en/class.datetimeimmutable.php

Z
Zhenya T, 2014-11-12
@Turowskiy

Thank you! I passed by it so many times, but always by)
I will add that the DateTimeImmutable::createFromMutable(new DateTime) method makes a "non-mutable" object out of a "mutable" object. Very convenient, but only works with version 5.6 :(

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question