1

I'm trying to display a time which is stored in MySQL database, with a specific timezone.

$time = '2014-01-20 21:40:13';

Is there any way to print this time in GMT+5.30 or Asia/Kolkata (timezone) ?

BlitZ
  • 12,038
  • 3
  • 49
  • 68
akhil.cs
  • 691
  • 1
  • 5
  • 12
  • What TZ a date in database is in? – zerkms Jan 21 '14 at 05:34
  • 1
    i got some records which is stored in my database, and i want to display the time in that records with a specific timezone, And i've seen "date_default_timezone_set()" but i dont think its enough for this.. – akhil.cs Jan 21 '14 at 05:35

4 Answers4

4

date_default_timezone_set( ) function with the requires time zone as argument in single quotes, all the date() following below will display the time in the requested time zone

<?php
    date_default_timezone_set("Asia/Kolkata");
    echo date('d-m-Y H:i:s'); //Returns IST
?>
Krish R
  • 22,583
  • 7
  • 50
  • 59
3

Object oriented style

<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";

$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>

Procedural style

<?php
$date = date_create('2000-01-01', timezone_open('Pacific/Nauru'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";

date_timezone_set($date, timezone_open('Pacific/Chatham'));
echo date_format($date, 'Y-m-d H:i:sP') . "\n";
?>

Documentation: http://us3.php.net/manual/en/datetime.construct.php

sunshinekitty
  • 2,307
  • 6
  • 19
  • 31
  • 1
    [straight from the documentation, just as i like it](http://us3.php.net/manual/en/datetime.construct.php) – Ohgodwhy Jan 21 '14 at 05:36
0

Have MySQL return the time in UNIX_TIMESTAMP() UNIX timestamp and use the PHP DateTime to format it to the timezone you want.

Tan Hong Tat
  • 6,671
  • 2
  • 27
  • 25
0
**Try the code........**

<?php date_default_timezone_set('America/Los_Angeles');?>

More detail here: http://www.php.net/manual/en/function.date-default-timezone-set.php
Indrajeet Singh
  • 2,958
  • 25
  • 25