Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get local timezone in PHP?

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.

like image 288
Steve Avatar asked Dec 27 '11 20:12

Steve


2 Answers

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"));
like image 187
Christopher Manning Avatar answered Oct 05 '22 22:10

Christopher Manning


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.

like image 40
Jason McCreary Avatar answered Oct 05 '22 22:10

Jason McCreary