Deewon
Deewon

Reputation: 33

Formatting the published result - Youtube API

I'm trying to get my date published to show as it does on youtube, ie. '2 days ago' or '4 hours ago'.

When I pull the date published it is just showing as '2012-01-11T20:49:59.00Z'

Here is my current code;

<?php
// set feed URL
$feedURL = 'https://gdata.youtube.com/feeds/api/users/RiotGamesInc/uploads?max-results=7';

// read feed into SimpleXML object
$sxml = simplexml_load_file($feedURL);
?>
<?php
// iterate over entries in feed
foreach ($sxml->entry as $entry) {

  // get nodes in media: namespace for media information
  $media = $entry->children('http://search.yahoo.com/mrss/');

  // get video player URL
  $attrs = $media->group->player->attributes();
  $watch = $attrs['url']; 

  // get video thumbnail
  $attrs = $media->group->thumbnail[1]->attributes();
  $thumbnail = $attrs['url']; 

  ?>
  <div class="youtubefeed">
    <a href="<?php echo $watch; ?>"><img src="<?php echo $thumbnail;?>" /></a></br>
    <?php echo $entry->published; ?>
  </div>
<?php
}
?>

Upvotes: 3

Views: 465

Answers (2)

Ivo Pereira
Ivo Pereira

Reputation: 3500

Are you looking for something like this?

<?php
function nicetime($date)
{
    if(empty($date)) {
        return "No date provided";
    }

    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");

    $now             = time();
    $unix_date         = strtotime($date);

       // check validity of date
    if(empty($unix_date)) {   
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {   
        $difference     = $now - $unix_date;
        $tense         = "ago";

    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }

    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }

    $difference = round($difference);

    if($difference != 1) {
        $periods[$j].= "s";
    }

    return "$difference $periods[$j] {$tense}";
}

$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago

?>

Upvotes: 0

Rolando Isidoro
Rolando Isidoro

Reputation: 5114

This as been asked several times before in stackoverflow, you could've just searched for "php time ago". Anyways, check the solution at http://www.php.net/manual/en/function.time.php#89415.

Upvotes: 2

Related Questions