格式化已发布的结果 - Youtube API

时间:2013-04-22 10:40:11

标签: php html xml youtube-api

我正在尝试将我的日期发布,以便在youtube上显示,即。 '2天前'或'4小时前'。

当我公布发布日期时,它显示为'2012-01-11T20:49:59.00Z'

这是我目前的代码;

<?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
}
?>

2 个答案:

答案 0 :(得分:2)

之前在stackoverflow中已经多次询问过,你可能只是搜索“php time ago”。无论如何,请检查http://www.php.net/manual/en/function.time.php#89415处的解决方案。

答案 1 :(得分:0)

你在找这样的东西吗?

<?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

?>