简体   繁体   中英

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!

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM