2

I want to localize date and time in php. I stored datetime in UTC in my database. And I want to:

  • display the time in the correct timezone
  • display the time in the correct language

For the first task, I use:

$time = //retrieve from database
$date_obj = new DateTime($time, new DateTimeZone("UTC"));
$date_obj->setTimeZone(new DateTimeZone($timezone));
$display_date = $date_obj->format($format); 

It works.

For the second task, after doing some research, it seems that I have to do something like this:

setlocale(LC_TIME, "de_DE"); //only necessary if the locale isn't already set
$formatted_time = strftime("%a %e.%l.%Y", $mytime->getTimestamp())

The problem is I don't know how to do the two things together.

Thank you!

user2335065
  • 2,337
  • 3
  • 31
  • 54
  • As suggested by @Pulkit, you can use date_default_timezone_set. See http://us1.php.net/date_default_timezone_set – Kaarel Nummert Jun 09 '14 at 09:12
  • I'm not sure what you mean by "do the two things together". Generally you will have to store both timezone and language in the user's profile preferences and do two the things one after another. Even if you knew the [Geo-location (GPS)](http://stackoverflow.com/questions/16086962/how-to-get-a-time-zone-from-a-location) you can derive the timezone and country but not the user's language as there are some countries with multiple languages being valid at the same time (e.g. http://en.wikipedia.org/wiki/List_of_multilingual_countries_and_regions) – xmojmr Jun 10 '14 at 12:58
  • @xmojmr, ya I know the two things are different. What I mean is as the $date_obj and $mytime are in different formats, how can I do the two things one after another? Thank you. – user2335065 Jun 11 '14 at 04:30
  • @user2335065 you mean one after another like in this http://stackoverflow.com/questions/10909911/php-setlocale-has-no-effect question or how can you split/parse/concatenate strings? – xmojmr Jun 11 '14 at 05:26

1 Answers1

0

Please Try This i think it may be helped you.

$timezone = "Asia/Kolkata";
date_default_timezone_set($timezone);

mysql_query("SET time_zone = '".date_default_timezone_get()."'");

Its execute in every page

Pulkit
  • 1