user1063287
user1063287

Reputation: 10879

how to convert youtube video published date to DD/MM/YY in php?

i am using the following to get the published date of a youtube video:

$url = "http://gdata.youtube.com/feeds/api/videos/{$random_text}?v=2&alt=json";
$json = file_get_contents($url);
$json = str_replace('$', '_', $json);
$obj = json_decode($json);
$video_date = $obj->entry->published->_t;

which ouputs the date in this format:

2012-10-18t13:04:42.000z

how can i convert this to the DD/MM/YY format in php?

i have tried the solution at:

What time format is this and how do I convert it to a standardized dd/mm/yyyy date?

$video_date_pre = $obj->entry->published->_t;
// format the video date
$video_date = date_format($video_date_pre, 'd/m/Y');

but i am getting the error:

Warning: date_format() expects parameter 1 to be DateTime..

thank you.

update

it may be relevant to note that the original source looks something like this (where you can search for "published"):

http://gdata.youtube.com/feeds/api/videos/eiAx2kqmUpQ?v=2&alt=json

Upvotes: 7

Views: 6308

Answers (1)

Stegrex
Stegrex

Reputation: 4034

Try this:

$video_date = date('d/m/y', strtotime($video_date_pre));

In this solution, you need to convert the string into Unixtime first, and then you can use the date() function.

http://php.net/manual/en/function.strtotime.php

http://www.php.net/manual/en/function.date.php

Or you can use the DateTime object:

$dateObject = new DateTime($video_date_pre);
$video_date = date_format($dateObject , 'd/m/y');

Upvotes: 9

Related Questions