[英]Comparing post date get_the_date() from today - Wordpress
I want to be able to calculate the days that post was created and compare with today to echo "Today/ Yesterday/ Last Week/ Last Month". 我希望能够计算帖子创建的日期并与今天进行比较,以回显“今天/昨天/上周/上个月”。 The date format I get from get_the_date() is "December 1, 2015" so I'm wondering if I need to use a different function that I don't know about. 我从get_the_date()获得的日期格式为“ 2015年12月1日”,所以我想知道是否需要使用我不知道的其他函数。
You just need to the get_the_date() function; 您只需要使用get_the_date()函数;
Now Date Should In YYYY-MM-DD Format 现在日期应采用YYYY-MM-DD格式
for that 为了那个原因
$date1 = date('Y-m-d', strtotime(get_the_date())) ;
$current_date1 = date('Y-m-d', time()) ;
Now use this function 现在使用此功能
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);
I'm not sure if there is any WordPress function but you can get your values using built in PHP functions. 我不确定是否有任何WordPress函数,但是您可以使用内置的PHP函数获取值。
Yesterday: 昨天:
date('Y-m-d', strtotime("-1 day"));
Last week 上个星期
date('Y-m-d', strtotime("-1 week +1 day"));
Last month 上个月
date('Y-m-'.1, strtotime("-1 month")); //First day of -1 month
You can read more about strtotime here http://php.net/manual/en/function.strtotime.php 您可以在http://php.net/manual/en/function.strtotime.php上阅读有关strtotime的更多信息。
Here is also a link to the date function, if you havent had any experience with it before: http://php.net/manual/en/function.date.php 如果您以前还没有使用过日期功能,那么这里也有一个链接: http : //php.net/manual/en/function.date.php
You would want to use Ymd format for wordpress querys read more about that here: http://php.net/manual/en/function.date.php 您可能希望将Ymd格式用于wordpress查询,在此处了解有关此内容的更多信息: http : //php.net/manual/en/function.date.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.