【问题标题】:Formatting the published result - Youtube API格式化发布的结果 - Youtube API
【发布时间】:2013-04-15 05:38:56
【问题描述】:

我正在尝试将我的日期发布为在 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
}
?>

【问题讨论】:

    标签: php html xml youtube-api


    【解决方案1】:

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

    【讨论】:

    • 谢谢,我不知道要搜索什么。
    【解决方案2】:

    你在寻找这样的东西吗?

    <?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-08-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多