qadenza
qadenza

Reputation: 9293

how to get date and time respecting user's timezone

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

Answers (3)

brutuscat
brutuscat

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:

  • You need to know how the date was stored, if you store all of it in UTC (e.g. no offset) then is simpler, but you might have issues when querying data if you need to access data assuming that have offset already.
  • MySQL does not stores TZ with its date-time value.
  • Make sure to instantiate the DateTime object using the proper timezone, PHP will use the default TZ if you don't pass any in the constructor. This is set on the php.ini and you can change it with this function date_default_timezone_set in your scripts.

Upvotes: 2

Raj Ravuri
Raj Ravuri

Reputation: 169

To get user or browser time. You must have their timezone, which you can get from Link.

Follow these steps:

  1. download code and call it by function call
  2. In first output you will see browser/user datetime, which you need to change accordingly(Ex: mm-dd-YY), using js bulit-in functions.
  3. Put outputted value into one js variable, and pass that value to form hidden input fields
  4. After form submission, capture that value as GET/POST to your database.

If you still fine hard time then, please let me know.

Upvotes: -1

Jason Joslin
Jason Joslin

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

Related Questions