This is really simple, but I can't seem to make it work. I want the Due Date text to be red if the current date is past due. I want it to be green if it is still not due yet. Also, I set the due date up to be 2 days past when the wordpress post is made. I need some help though, it all looks good to me, but everything is green regardless of if it is past due or not.

    $current_date = strtotime(date( 'F j, Y g:i A' ));
    $expire_date = get_the_date( 'F j, Y g:i A' );
    $expire_date = new DateTime($expire_date);
    $expire_date->add(new DateInterval('P2D'));

    $compare_date = $expire_date->format('F j, Y g:i A');
<?php if( get_field( 'maxpreps_link' ) or get_field( 'khsaa_link' ) ) { echo "<span style='color: green;'>Done!</span>"; } ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" target="_blank" style="color: #0000EE;"><?php the_title(); ?></a>
&nbsp;&nbsp;<span>(<?php echo the_date(); ?>)</span>
echo ("&nbsp;&nbsp;" . "<span style='float: right; color: "); 
if( $compare_date > $current_date ) { 
    echo "#BA0000"; } 
else { echo "#39AC00"; } 
echo (";'>Due Date: " . $compare_date . "</span></strong>");

1 Answer 1


$current_date is a timestamp and $compare_date is a formatted date, so you're comparing different things.

As you're already using DateTime and as of PHP 5.2.2 DateTime objects can be compared using comparison operators, you can compare instead $expired_date (DateTime object) with today's date as a DateTime object:

$current_date = new DateTime();
$expired = new DateTime();
$expired->setTimestamp( get_the_time('U') );  // get UNIX timestamp of current post
$expired->add(new DateInterval('P2D'));  // add 2 days

$day_color = '';
if( $current_date > $expired ) {
    $day_color = '#BA0000';
} else {
    $day_color = '#39AC00';

echo sprintf( '<span style="float: right; color: %s;">Due Date: %s</span>', $day_color, $expired->format('F j, Y g:i A') );
  • Did this helped?
    – dbeja
    Jul 21, 2017 at 23:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.