我想解析Twitter API created_at值(存储到变量中),如下所示:
Sun Aug 28 19:31:16 +0000 2011
进入这个:
19:31,Aug 28
但我需要它是时区意识.有关如何使用PHP进行此操作的任何想法?
使用John Flatness建议的第二个选项后,我收到此错误:
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's 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
如果使用
date_default_timezone_set
(或date.timezone INI设置)设置要输出的时区,则可以执行以下操作:
$formatted_date = date('H:i,M d',strtotime('Sun Aug 28 19:31:16 +0000 2011'));
如果您需要在许多不同的时区输出,那么使用新式DateTime
类可能更容易:
$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');