user606263
user606263

Reputation:

Wordpress: Check if date is today or has passed

I have a custom field that spits out the date in the format dd/mm/yy

I am using this to display it on the front end...

<?php echo "<p>" . get_post_meta($post->ID, 'duedate', TRUE) . "</p>"; ?>

...which outputs...

<p>02-11-2013</p>

But what I need to do is insert a class if the date is todays date or in the past, like:

<p class="overdue">02-11-2013</p>

Thanks!

Upvotes: 0

Views: 3993

Answers (1)

Patrick Moore
Patrick Moore

Reputation: 13344

What constitutes overdue? This code should work to compare if the date provided in meta value is yesterday or newer.You can adjust the -1 days parameter to get the appropriate date.

<?php
$date = get_post_meta($post->ID, 'duedate', TRUE); // Pull your value
$datetime = strtotime( $date ); // Convert to + seconds since unix epoch
$yesterday = strtotime("-1 days"); // Convert today -1 day to seconds since unix epoch
if ( $datetime >= $yesterday ) { // if date value pulled is today or later, we're overdue
    $overdue = ' class="overdue"';
} else {
    $overdue = '';
}
?>
<p<?php echo( $overdue ); ?>
<?php echo( $date ); ?>
</p>

Upvotes: 2

Related Questions