What you did wrong here is the part date('d', $rem_days)
. The function date()
should be used to convert timestamp to a formatted date, not to converting a time difference in timestamp to time difference in days.
You can fix this by replacing date('d', $rem_days)
with floor($rem_days/86400)
.
The complete code should be:
$event_date = strtotime( get_field( 'event_date', false, false ) );
$curr_date = time();
$rem_days = $event_date - $curr_date;
if ( $rem_days <= 0 ) {
$event_msg = '<strong>Event Expired</strong>';
} else {
$event_msg = '<strong>' . floor( $rem_days / 86400 ) . '</strong> Days Remaining';
}
Note: As per Pat J's comment, WordPress provides a bunch of handy {timespan}_IN_SECONDS
constants in wp-includes/default-constants.php
, including DAY_IN_SECONDS
.
So in the above code, floor( $rem_days / 86400 )
can be replaced with floor( $rem_days / DAY_IN_SECONDS )