Inside my content-single
template, I have a conditional check that displays text based on how old the post is. The idea is to inform visitors that they might be reading "old" news.
Problem is; the 6+ months old
text is displayed on posts that are only days old, and I do not understand why.
This is the code I am using:
<?php
if ( strtotime( get_the_date() ) < strtotime( '-1 year' ) ) { ?>
<span class="old-post"> 6+ months old </span> /
<?php
}
?>
This is the format I am using in WP admin for date and time:
l \t\h\e jS \of F @ H:i
Please help me understand how to fix this.
Inside my content-single
template, I have a conditional check that displays text based on how old the post is. The idea is to inform visitors that they might be reading "old" news.
Problem is; the 6+ months old
text is displayed on posts that are only days old, and I do not understand why.
This is the code I am using:
<?php
if ( strtotime( get_the_date() ) < strtotime( '-1 year' ) ) { ?>
<span class="old-post"> 6+ months old </span> /
<?php
}
?>
This is the format I am using in WP admin for date and time:
l \t\h\e jS \of F @ H:i
Please help me understand how to fix this.
Share Improve this question asked Feb 13, 2021 at 5:55 Harold AldersenHarold Aldersen 278 bronze badges2 Answers
Reset to default 3The problem is that your date format is not a standard format that strtotime()
understands. Instead just use get_the_date()
with the format set to 'U'
, which returns the timestamp, and compare that to strtotime( '-1 year' )
:
if ( get_the_date( 'U' ) < strtotime( '-1 year' ) ) {
}
By using below code, you can check if Post Was published more than 6 Months ago using get_the_date.
place below code inside content-single template, It inform visitors that they are reading "old" news which is plublished 6+ months old.
# get last 6 month date from current date
$last_sixth_month_date = strtotime("-6 month");
# publish date
$post_poblish_date = strtotime( get_the_date() );
# get difference from two dates
$year1 = date('Y', $last_sixth_month_date);
$year2 = date('Y', $post_poblish_date);
$month1 = date('m', $last_sixth_month_date);
$month2 = date('m', $post_poblish_date);
$month_diffrence = (($year2 - $year1) * 12) + ($month2 - $month1);
if($month_diffrence < 0 )
{
# check difference between date is 6 or more than 6
if(abs($month_diffrence) >= 6 )
{
echo '<span class="old-post"> 6+ months old </span> / ';
}
}