Reputation: 9293
I'm totally confused about php/mysql date/time functions.
My date
column is timestamp
- defalult value - current_timestamp
.
date_default_timezone_set("Europe/Belgrade"); //my timezone
$stmt = $db->query("SELECT * FROM cinema order by date desc");
while($row = $stmt->fetch()){
$date = strtotime($row['date']);
$datea = date("d-m-y", $date);
$time = date("H:i", $date);
...
I'm getting the difference of -2 hours comparing to my local time.
How can I get the correct time (respecting my timezone), and if possible each user, whereever he is located on the globe, to get this data respecting his own timezone?
Upvotes: 0
Views: 439
Reputation: 3130
MySQL does not saves time zones at all. Depending for what you'd use the data stored, you should either always store the date-time value in "UTC" (this is like GMT, with no TZ offset at all) or in some cases you would like to store it with the offset already set. But for the second option, you should also store the TZ or the offset somewhere else (again MySQL date-time is unable to store this) in order to properly reconstruct the object from the DB.
For example, I usually add a created_at, updated_at fields in my db records and I store this in UTC, for this task I use PHP's gmdate('Y-m-d H:i:s')
Now, if you have to show this date-time in the user's TZ, you just have to create a DateTime object like so and change the TZ:
$createdAt = new DateTimeImmutable(
$row->created_at,
new DateTimeZone('UTC')
);
For the second case, where you could store the date-time with additional piece of data where you also save the TZ, you have to make sure to instantiate the DateTime object using the timezone constructor second argument to move the offset properly. So for a person from Argentina that will travel to Madrid, you might want to store the checkin date in the TZ of the destination (e.g. Madrid), but for displaying purposes, (e.g. want to know if might be able call in a normal time and let the family know that he/she arrived Ok) you later convert it to the origin tz:
$checkinAt = new DateTimeImmutable(
$row->checkin_at,
new DateTimeZone('Europe/Madrid')
);
$checkinAt->setTimezone('America/Argentina/Buenos_Aires');
To sum up:
date_default_timezone_set
in your scripts.Upvotes: 2
Reputation: 169
To get user or browser time. You must have their timezone, which you can get from Link.
Follow these steps:
If you still fine hard time then, please let me know.
Upvotes: -1
Reputation: 1144
you can do it pretty sweetly just in the sql query
CONVERT_TZ (dt, from_tz,to_tz)
http://www.w3resource.com/mysql/date-and-time-functions/mysql-convert_tz-function.php
$stmt = $db->query("SELECT *, CONVERT_TZ(date, "+00:00", "+01:00") FROM cinema order by date desc");
^ Assuming your database is UTC time and the time you are trying to convert to is "Europe/Belgrade".
First parameter is your datetime stamp, second is your database timezone and the third is your time you want it to convert to. If the timezone change is for the where/conditional part of the query you would put your from and to the other way around.
Upvotes: 1