1

我正在使用以下

<?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['post_date'] - 它出现在 2012-12-03 13:59:56 - 我只想在 2012 年 12 月 3 日看到这个。我不知道该怎么走关于它。我知道还有其他一些与此类似的解决方案,但我对此并不陌生,并且真的不理解它们......?

帮助?

谢谢。

4

1 回答 1

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

于 2012-12-04T14:23:51.493 回答