Twitter json + php
I am pulling the twitter json feed and parsing it with php.
One of the values in the array is [created_at] => Wed Jun 22 06:19:31 +0000 2011
.
However I only want the "Wed Jun 22
".
I am currently using $data->created_at = substr($data->created_at, 0, 10);
but 开发者_开发知识库for example if a month/day has 4 characters it will mess it up so I need a better way to do this.
preg_match("/([a-zA-Z]+ [a-zA-Z]+ [0-9]+)/", "Wed Jun 22 06:19:31 +0000 2011", $date);
$date = $date[1];
Or you can use strtotime to create a new date with php date function.
$new_date = date('D M n', strtotime($data->created_at));
You should look into PHP's DateTime class, especially DateTime::createFromFormat(). Creating a date/time object way cleaner solution than using substr()
.
Plus, you know, if you ever wanted to store year...
echo date('D M d',strtotime($array['created_at']));
DEMO
精彩评论