因此,我在一个方法中包含以下查询,该方法可获取5个最新帖子。
public function get_most_recent()
{
return BlogPost::get([
'post_status' => 'publish',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => 5,
]);
}
如何获取最新的职位年份?我有一种方法可以使用date('Y')获取当前年份,但是我想获取最新的发布年份。我将如何去做?
这是我的方法:
public function get_most_recent_year()
{
$posts = $this->get_most_recent();
foreach ($posts as $post) {
var_dump($post);
}
}
如果可能的话,我想瞄准$year = get_most_recent_year()
;
更新:
var_dump($ post)给我以下结构:
对象
-受保护的“帖子” =>
-对象
---私人'date'=>
-----对象
------ public'date'=>字符串'2017-09-14 09:45:53.000000'
答案 0 :(得分:0)
好像您正在使用WordPress。您可以使用get_the_date()
获取帖子的日期。第一个参数是PHP日期格式:
$post_year = get_the_date('Y', $latest_post_id);
答案 1 :(得分:0)
解析日期并从中获取年份。
public function get_most_recent_year()
{
$posts = $this->get_most_recent();
if (empty($posts)) {
return 0;
}
$post = $posts[0];
$year = $post[0]->post->date->format('Y');
return $year;
}
答案 2 :(得分:0)
要获取时间,可以使用以下语句。 只是简单地解析日期
<?php $time = date("Y", strtotime($row['PostDate'])); ?>