我希望能够计算出创建帖子的天数,并与今天进行比较,以回应“今天/昨天/上周/上个月”。我从get_the_date()得到的日期格式是“2015年12月1日”,所以我想知道我是否需要使用一个我不知道的不同函数。
发布于 2015-12-31 07:36:45
您只需要使用get_the_date()函数;
现在日期应为YYYY-MM-DD格式
为此,
$date1 = date('Y-m-d', strtotime(get_the_date())) ;
$current_date1 = date('Y-m-d', time()) ;
现在使用此函数
function dateDifference($date_1 , $date_2 )
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
return $interval->format('%a');
}
//call above function
echo $days = dateDifference($date1, $current_date1);
发布于 2015-12-31 07:36:54
我不确定是否有WordPress函数,但您可以使用内置函数获取值。
昨天:
date('Y-m-d', strtotime("-1 day"));
上周
date('Y-m-d', strtotime("-1 week +1 day"));
上个月
date('Y-m-'.1, strtotime("-1 month")); //First day of -1 month
您可以在这里阅读有关strtotime的更多信息http://php.net/manual/en/function.strtotime.php
这里还有一个指向date函数的链接,如果您以前没有使用过它的话:http://php.net/manual/en/function.date.php
您可能希望使用Y-m-d格式进行wordpress查询,请在此处阅读更多信息:http://php.net/manual/en/function.date.php
https://stackoverflow.com/questions/34542524
复制