In PHP we usually use the DateTime functions for most of the date operations. The PHP DateTime are Mutable dates. The Mutable dates can be a source of confusion and they make unexpected bugs in your code. In this blog, we discuss mutable date issues and possible solutions. Show
Issue with DateTimeBelow example, we going to display the today & tomorrow date using the DateTime object. <?php If you execute the above code you will get the same date for both today & tomorrow. Because of the Try Medium new feature of a code block with Syntax highlighterSyntax highlighter on Mediumbalajidharma.medium.com Maybe issue with <?php The above example also returns the same date. So you will get the same instance if you use mutable DateTime. How to find the number of days between two dates in PHPGet the Number of Days Between Two Datesblog.devgenius.io How to fix this issuePhoto by James Kovin on UnsplashA simple fix is to move the $today_date echo statement before <?php 0, <?php 1 methods.<?php But all the time we can't use the above fix. Because sometimes we need to assign the values to views. That time both will have the same date. PHP object cloningWe can fix the issue by using PHP object cloning. Now try the clone keyword to clone the DateTime object. Then modify or add your changes. <?php Object cloning makes your code more complex and introduces unnecessary noise. Also difficult to call clones on all the time. This issue is not only for <?php 0 and <?php 1 functions. You will face the same issue on the below <?php 4 functions.
DateTimeImmutableThe <?php 2 class behaves the same as DateTime except new objects are returned when modification methods such as DateTime::modify() are called. In PHP 5.5 the DateTimeImmutable class was introduced.
The DateTime and DateTimeImmutable are implemented from <?php 3 interface.Let's try <?php 2 our examples.<?php The above code will print the correct today and tomorrow dates. Instead of cloning, this DateTimeImmutable instance is the easy and perfect solution for our issue. Another example with add() function <?php DateTime librariesPHP Carbon is one of the best DateTime libraries. The carbon also provides the <?php 5 object. So use the immutable object if you using the carbon library.<?php ConclusionSo consider using the immutable DateTime object to avoid certain types of errors with a mutable DateTime. If any specific requirements use the mutable object, otherwise use the <?php 2
How to echo current time in PHP?echo "The time is " . date("h:i:sa"); ?> Note that the PHP date() function will return the current date/time of the server!
How to get date time string in PHP?Answer: Use the PHP date() Function
You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.
How to print date object in PHP?$d=mktime(11, 14, 54, 8, 12, 2014); echo "Created date is " . date("Y-m-d h:i:sa", $d);
How to get date and time in PHP?PHP Date and Time.. PHP | checkdate() Function.. PHP | date_create(), date_format(), date_add() Functions.. PHP | date_create_from_format() Function.. PHP | date_date_set() Function.. PHP | date_default_timezone_get() Function.. PHP | date_default_timezone_set() Function.. PHP | date_diff() Function.. |