Skip to main content
Add the use of WordPress time constant
Source Link

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 )

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';
}

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 )

Correcting the explanation of date() function
Source Link

What you did wrong here is the part date('d', $rem_days).

When you use something like that, if you have the number of days more than one month, the The function date() with ignore that and only count the number of days. So for example, when the duration is 1 month and 14 daysshould be used to convert timestamp to a formatted date, it will instead show 14 daysnot to converting a time difference in timestamp to time difference in days.

You can fix this by replacing date('d', $rem_days) with floor($remaining_time$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';
}

What you did wrong here is the part date('d', $rem_days).

When you use something like that, if you have the number of days more than one month, the function date() with ignore that and only count the number of days. So for example, when the duration is 1 month and 14 days, it will instead show 14 days.

You can fix this by replacing date('d', $rem_days) with floor($remaining_time/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';
}

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';
}
Source Link

What you did wrong here is the part date('d', $rem_days).

When you use something like that, if you have the number of days more than one month, the function date() with ignore that and only count the number of days. So for example, when the duration is 1 month and 14 days, it will instead show 14 days.

You can fix this by replacing date('d', $rem_days) with floor($remaining_time/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';
}