尝试date_diff()
/ DateTime::diff()
PHP中的函数:
// Object-oriented style.
$datetime1 = new DateTime( $post->post_date );
$datetime2 = new DateTime(); // current date
$interval = $datetime1->diff( $datetime2 );
echo $interval->format( \'%a days old\' );
// .. or procedural style.
$datetime1 = date_create( $post->post_date );
$datetime2 = date_create(); // current date
$interval = date_diff( $datetime1, $datetime2 );
echo $interval->format( \'%a days old\' );
在PHP 5.2.2或更高版本中,可以比较日期(“前”和“后”),如下所示:
if ( $datetime1 < $datetime2 ) {
echo $interval->format( \'Published %a days ago.\' );
} elseif ( $datetime1 > $datetime2 ) {
echo $interval->format( \'Will be published in %a days later.\' );
} else {
echo \'Published today.\';
}