0

I'm currently storing dates in my MYSQL database as a PHP timestamp (integer field). However, if I retrieve it from the database and use the PHP date function, would it display the correct time based on the client's time zone.

For example, I display a PHP timestamp 1310674625 (pulled from DB) which is Jul 14 2011 22:17:05 in Eastern Time. So if I display the timestamp in PHP with the code below, would a person viewing the webpage in the Pacific Time Zone see Jul 14 2011 19:17:05 instead?

echo date('M d Y H:i:s', 1310674625);
  • i dont think it will work like that, why dont u save it as UTC and use javascript to determine the client timezone and format it correctly – Ibu Jul 14 '11 at 22:20

2 Answers2

0

No. The date will be displayed according to the server's time and timezone, not the user's.

You can offer an option on your site to allow the user provide his/her timezone (or attempt to use geolocation based on the IP) and adjust the output according.

According to this post, reliably determining the user's timezone may not be possible, so for best results, you may need to get input from the user.

EDIT (Based on the request for more information in the comments)

If you need to output the selected timezone, you can use one of the following options:

echo date('e'); // Examples: UTC, GMT, Atlantic/Azores (Available in version 5.1.0 and later
echo date('O'); // Different in Greenwich meantime. Example: +0200
echo date('T'); // Timezone abbreviation. Examples: EST, MDT ...

See the date manual page for more information.

EDIT 2:

To "print the date to a specified timezone" you will need to:

  1. Get the current date as a timestamp (time())
  2. Get the user's timezone offset
  3. Use mktime to add or subtract the user's offset as needed.
  4. Convert the result back to human-readable format using date().
Community
  • 1
  • 1
George Cummins
  • 28,485
  • 8
  • 71
  • 90
-1

Use date_default_timezone_set if you know users timezone

Also you can use Javascript Date(print timestamp here)

RiaD
  • 46,822
  • 11
  • 79
  • 123