I have a problem in DateTime sql

When I add a new row with the current time, the time is added 7 hours late
Is there a solution to this problem

Welcome to the forum!

MySQL uses UTC to store the DateTime. If you live in another timezone, it will be different.

5 Likes

i’m in Libya, can i fix this?

Instead of using CURRENT_TIMESTAMP(), you can submit data to the database using PHP or Javascript. This post explains it better than I do:

But instead of storing the local time, you can convert the UTC to local time as described in this other StackOverflow post:

7 Likes

$dt = new DateTime(‘now’, new DateTimeZone(‘UTC’));

$dt->setTimezone(new DateTimeZone(‘Africa/Tripoli’));

$formattedDateTime = $dt->format(‘Y-m-d H:i:s T’);

echo $formattedDateTime;

Thx Its work 100% with a little edit

Thx sir

2 Likes

MySQL DATETIME fields are not timezone aware. You, the developer, have to make sure that the time zoning is correct.

If you ask MySQL for the current time, it will give you the system time. On free hosting, that’s set to EST (UTC-5). And because it’s the system time, you cannot change it, because it can only be changed for the entire server.

For this reason, it’s better to take the current time from PHP, because in PHP you can use timezones and set a default time zone.

6 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.