0

我有一个自定义字段以 dd/mm/yy 格式吐出日期

我正在使用它在前端显示它......

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

...输出...

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

但是,如果日期是今天或过去,我需要做的是插入一个类,例如:

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

谢谢!

4

1 回答 1

1

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>
于 2013-11-02T03:41:26.220 回答