DateTimeImmutable

We can use DateTimeImmutable in place of DateTime class when we want the original object is not to be modified.

Using date add ...
$date1=new DateTime('2018-04-23'); // date object created. 
$date1->add(new DateInterval('P1Y3M')); // interval of 1 year 3 months added
echo $date1->format('Y-M-d'); // Output is 2019-Jul-23
/// by using DateTimeImmutable ///
$date1=new DateTimeImmutable('2018-04-23'); // date object created . 
$date1->add(new DateInterval('P1Y3M')); // interval of 1 year 3 months added
echo $date1->format('Y-M-d'); //  Output is 2018-Apr-23
In case of date object created using DateTimeImmutable after adding the interval the original value never changes.

PHP Date Functions setDate() : Set date for date object Timestamp in PHP Displaying calendar for selection of date by user
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer