3

我正在尝试发布我的日期以显示它在 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
}
?>
4

2 回答 2

2

正如之前在 stackoverflow 中多次询问的那样,您可以搜索"php time ago"。无论如何,请在http://www.php.net/manual/en/function.time.php#89415检查解决方案。

于 2013-04-22T13:52:17.727 回答
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

?>
于 2013-04-22T13:58:55.423 回答