Twitter API parsing created_at

I want to analyze the value of the Twitter created_at API (stored in a variable), which looks like this:

Sun Aug 28 19:31:16 +0000 2011

In it:

19:31, August 28

But I need him to be aware of the time zone. Any ideas on how to do this with php ?


After using the second option, John Flatness suggested that I get this error:

 Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [<a href='datetime.--construct'>datetime.--construct</a>]: It is not safe to rely on the system timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/Sao_Paulo' for 'BRT/-3.0/no DST' instead' in /misc.php:4 Stack trace: #0 /misc.php(4): DateTime->__construct('Sun Aug 28 19:3...') #1 /tabs/home.php(29): format_date(Object(SimpleXMLElement), 'America/Sao_Pau...') #2 {main} thrown in /misc.php on line 4 
+7
source share
1 answer

If you set the time zone that you want to display using date_default_timezone_set (or set the INI for date.timezone), you can simply do:

 $formatted_date = date('H:i, M d', strtotime('Sun Aug 28 19:31:16 +0000 2011')); 

If you need to potentially display data in many different time zones, it might be easier to use the new-style DateTime class:

 $date = new DateTime('Sun Aug 28 19:31:16 +0000 2011'); $date->setTimezone(new DateTimeZone('America/New_York')); $formatted_date = $date->format('H:i, M d'); 

Obviously, the 'America/New_York' part would actually be a setting for each user for their time zone, not a literal string.

+21
source

All Articles