所以我在一個獲取 5 個最新帖子的方法中有以下查詢。public function get_most_recent(){ return BlogPost::get([ 'post_status' => 'publish', 'order' => 'DESC', 'orderby' => 'date', 'posts_per_page' => 5, ]);}如何獲取最新的發布年份?我有一種使用日期('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) 給了我以下結構:對象-受保護的'post'=>--對象--私有'日期'=>-----對象------公共'日期'=>字符串'2017-09-14 09:45:53.000000 'php
3 回答

臨摹微笑
TA貢獻1982條經驗 獲得超2個贊
解析日期并從中獲取年份。
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;
}

MMMHUHU
TA貢獻1834條經驗 獲得超8個贊
看起來您正在使用 WordPress。您可以使用get_the_date()
來獲取帖子的日期。第一個參數是 PHP 日期格式:
$post_year = get_the_date('Y', $latest_post_id);

Cats萌萌
TA貢獻1805條經驗 獲得超9個贊
要獲得時間,您可以使用以下語句。只需簡單地解析日期
<?php $time = date("Y", strtotime($row['PostDate'])); ?>
- 3 回答
- 0 關注
- 127 瀏覽
添加回答
舉報
0/150
提交
取消