0

I've an application,say the end user might be from any country but when he does some action,i want the date to be shown in a particular time zone.

I want insert this into DB so i'm doing this using date_default_timezone_set(''); with date()

Is this the right way or should i use gmdate() and add time zone.

Thanks

4
  • Of side note, realize that time in PHP is based on the webserver, not where the client is located. Commented Nov 4, 2012 at 4:12
  • @TimDearborn:So i can get the server time and add/subtract according to my time zone,will this work ? Commented Nov 4, 2012 at 4:26
  • Some of the time. Different settings for Daylight Savings Time as such could throw it off at various times of the year. You would want to use gmdate() and then adjust for your timezone, as Jesse Bunch mentioned in his answer. Commented Nov 4, 2012 at 4:33
  • @TimDearborn:I know to use gmdate(),how to adjust to my time zone ? Commented Nov 4, 2012 at 4:35

2 Answers 2

1

No, don't use date_default_timezone_set() for timezone conversions. This can have unintended side-effects.

Instead, use this:

$tz = new DateTimeZone('America/Los_Angeles');

$date = new DateTime('Thu, 31 Mar 2011 02:05:59 GMT');
$date->setTimeZone($tz);
echo $date->format('l F j Y g:i:s A I')."\n";

Note, you're creating the DateTime object using UTC time and then applying the timezone. This way is much cleaner.

Sign up to request clarification or add additional context in comments.

1 Comment

:Now when ever any one in the world uses my app,it always store date in my time zine,i mentioned,am i right.Can you see Tim Dearborn comment.
0

If you want GMT, I think you should use the right Timezone as Europe/London

  date_default_timezone_set('Europe/London');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.