10

I'm using the date("D, m/d/Y h:i:s A T") function call to get current date time but returned time is different to the time I get when I make date in linux bash command (local time is in CST and php date function returns time in UTC). I've tried using $timezone = @system("date +%Z",$retval); to get timezone in an attempt to set local timezone using date_default_timezone_set() function. But CST seems not a valid timezone_identifier.

In short, I just need to get the same date and time as local datetime.

2
  • 2
    Again, please stop signing your posts. Dec 27, 2011 at 20:34
  • Ok. I didn't know it was forbidden.
    – Stephane
    Dec 27, 2011 at 22:41

3 Answers 3

13

Use a timezone from http://php.net/manual/en/timezones.php

So, for CST use:

<?php
date_default_timezone_set("America/Chicago");

If you only have the timezone abbreviation and not the timezone id then use:

<?php
date_default_timezone_set(timezone_name_from_abbr("CST"));
5
  • +1 for being first. Note that the user may not be Chicago. Although indeed CT, it's best to use your specific locale. Dec 27, 2011 at 20:34
  • the issue is I need it done programmatically so I can't go and copy the exact valid timezone_identifier each time I need it. The only value I have is CST or UTC and so on...
    – Stephane
    Dec 27, 2011 at 22:14
  • @StephaneKouakou I've added sample code for when you only have the timezone abbreviation Dec 27, 2011 at 23:00
  • 1
    I think I've found a better function: timezone_name_from_abbr()
    – Stephane
    Dec 27, 2011 at 23:16
  • 1
    if you want to grab the default from *nix server, timezone_name_from_abbr(exec('date +%Z')) saves you from having a static entry
    – Praesagus
    Apr 4, 2013 at 0:15
2

Although the system and PHP should be on the same timezone, read the docs for date_default_timezone_set() to set the timezone manually.

Note that CST is not a valid timezone setting. Find a valid timezone for your location.

0

I think I've found a better function: timezone_name_from_abbr()

1
  • If this turned out to be the correct answer, please mark it as correct.
    – rvighne
    Jan 20, 2014 at 16:00

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.