我正在使用下面的代码
<?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日。我不知道该怎么做。我知道还有其他一些与此类似的解决方案,但我是新手,真的不了解它们...?
帮助?
谢谢。
发布于 2012-12-04 22:23:51
在PHP中,date()函数提供了很多格式化的可能性。您要做的是使用以下语句:
echo date("F j, Y", $post['post_date']);这里
对应于full textual representation of a month, such as January or March
Day of the month without leading zeros
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']));为了让date()正常工作,函数strtotime()首先会将您现有的日期转换为时间戳。
有关strtotime()的更多信息,请点击此处:http://php.net/manual/en/function.strtotime.php
https://stackoverflow.com/questions/13705029
复制相似问题