2012-12-04 46 views
1

我使用以下格式化日期輸出 - PHP

<?php 
function custom_echo($x) 
{ 
    if(strlen($x)<=150) 
    { 
    echo $x; 
    } 
    else 
    { 
    $y=substr($x,0,150) . '...'; 
    echo $y; 
    } 
} 

// Include the wp-load'er 
include('../../blog/wp-load.php'); 

// Get the last 10 posts 
// Returns posts as arrays instead of get_posts' objects 
$recent_posts = wp_get_recent_posts(array(
    'numberposts' => 4 
)); 

// Do something with them 
echo '<div>'; 
foreach($recent_posts as $post) { 
    echo '<a class="blog-title" href="', get_permalink($post['ID']), '">', $post['post_title'], '</a><br />', $post['post_date'], custom_echo($post['post_content']), '<br /><br />'; 
} 
echo '</div>'; 
?> 

什麼我遇到的問題是$信息[「POST_DATE」] - 它出來爲2012年12月3日13: 59:56 - 我只想在2012年12月3日閱讀。我不知道如何去做。我知道還有一些其他解決方案與此類似,但我對此並不瞭解,並且真的不理解他們......?

幫助?

謝謝。

回答

8

在PHP中,date()函數帶有很多格式化的可能性。你想要做的是使用以下語句:

echo date("F j, Y", $post['post_date']); 

這裏

  1. 'F' 對應一個full textual representation of a month, such as January or March
  2. 'J' 相當於Day of the month without leading zeros
  3. 'Y' 相當於A full numeric representation of a year, 4 digits

您可以在文檔中找到更多信息和格式在這裏:http://php.net/manual/en/function.date.php

編輯:如果您的變量$post['post_date']包含現有日期,你應該這樣做,而不是:

echo date("F j, Y", strtomtime($post['post_date'])); 

功能strtotime()會先轉換你的日期在一個時間戳date()正常工作。在這裏strtotime()

更多信息:http://php.net/manual/en/function.strtotime.php

+0

這是有趣的 - 我看到文件,但不知道如何實現它。我試過這個說法,但1970年1月1日所有博客文章? – imcconnell

+2

@ user1802256:嘗試做'echo date(「F j,Y」,strtotime($ post ['post_date']));' –

+0

Yep @RocketHazmat是正確的我編輯我的帖子來反映這一點。 – koopajah