Avoid this when using Date/Time functions in PHP

Is there any difference between getting the current date/time in the following ways in PHP?

(new \DateTimeImmutable())->format(‘Y-m-d H:i:s’);

\DateTimeImmutable::createFromFormat(‘U’, (string) time())
->format(‘Y-m-d H:i:s’);


This content originally appeared on DEV Community and was authored by Marcos Rezende

Is there any difference between getting the current date/time in the following ways in PHP?

(new \DateTimeImmutable())->format('Y-m-d H:i:s');
\DateTimeImmutable::createFromFormat('U', (string) time())
    ->format('Y-m-d H:i:s');

Imagine that you are inserting data into a database table using the first way, but when querying the same database table you use the second option.

While the first option will return the current date/time of your server taking into account its Time Zone configuration, the second way will return the current date/time of your server based on UTC time zone.

After creating just a single user into a users table with this data

$user->name = 'Sebastian';
$user->createdAt = (new \DateTimeImmutable())
    ->format('Y-m-d H:i:s');

and querying the user table in this way

$dql = <<<EOT
        SELECT u FROM App\Entity\User u
        WHERE u.createdAt <= :createdAt
    EOT;

$now = \DateTimeImmutable::createFromFormat('U', (string) time())
    ->format('Y-m-d H:i:s');

$users = $this->entityManager()
    ->createQuery($dql)
    ->setParameter('createdAt', $now)
    ->getResult();

you will receive an empty array of $users if the server which hosts your application is located in any country with Time Zone configuration greater than 0 (Lisbon, France, Germany, and so on).

It will happen because the data inserted into your user table will have the createdAt field was filled with 18:14 while when you try to query the database, you will use the time 16:14.

You didn't create any user before 16:14!

You will never find the users that you recently created.

This happens because when you use U for formatting the date/time values, you are getting a Unix Timestamp date7time format which gets the date/time always based on Coordinated Universal Time, and never taking into account your Server's Time Zone configuration.

It looks pretty basic but maybe you have never had this issue because you are working bellow UTC Time Zone (Denmark, South America, Canada, USA, and so on).


This content originally appeared on DEV Community and was authored by Marcos Rezende


Print Share Comment Cite Upload Translate Updates
APA

Marcos Rezende | Sciencx (2021-08-28T16:37:31+00:00) Avoid this when using Date/Time functions in PHP. Retrieved from https://www.scien.cx/2021/08/28/avoid-this-when-using-date-time-functions-in-php/

MLA
" » Avoid this when using Date/Time functions in PHP." Marcos Rezende | Sciencx - Saturday August 28, 2021, https://www.scien.cx/2021/08/28/avoid-this-when-using-date-time-functions-in-php/
HARVARD
Marcos Rezende | Sciencx Saturday August 28, 2021 » Avoid this when using Date/Time functions in PHP., viewed ,<https://www.scien.cx/2021/08/28/avoid-this-when-using-date-time-functions-in-php/>
VANCOUVER
Marcos Rezende | Sciencx - » Avoid this when using Date/Time functions in PHP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/28/avoid-this-when-using-date-time-functions-in-php/
CHICAGO
" » Avoid this when using Date/Time functions in PHP." Marcos Rezende | Sciencx - Accessed . https://www.scien.cx/2021/08/28/avoid-this-when-using-date-time-functions-in-php/
IEEE
" » Avoid this when using Date/Time functions in PHP." Marcos Rezende | Sciencx [Online]. Available: https://www.scien.cx/2021/08/28/avoid-this-when-using-date-time-functions-in-php/. [Accessed: ]
rf:citation
» Avoid this when using Date/Time functions in PHP | Marcos Rezende | Sciencx | https://www.scien.cx/2021/08/28/avoid-this-when-using-date-time-functions-in-php/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.