Reputation: 163
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.
Upvotes: 2
Views: 232
Reputation: 6992
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...
Upvotes: 0
Reputation: 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));
Upvotes: 0
Reputation: 50966
preg_match("/([a-zA-Z]+ [a-zA-Z]+ [0-9]+)/", "Wed Jun 22 06:19:31 +0000 2011", $date);
$date = $date[1];
Upvotes: 1