Delete expiration date information in Wordpress posts

2

I'm using the code below to tell how long it takes for a post to expire on my Wordpress website, whose expiration value I set in a Custom Field (expiration).

From this code, since I prevent a post from expiring in 4 hours, do not display information like this: "Expires in 0 days, 4 hours and 49 minutes".

I do not want "0 days" to be displayed, just the remaining hours. In case the post expires the next day, then okay, it could be displayed normally "Expires in 1 days, 4 hours and 49 minutes".

In addition, there are posts that I do not define expiration, and in all of them the following information appears: "Expires in 0 days, 2 hours and 0 minutes". How do I display information such as "Unspecified expiration" for these posts?

           <?php 
                $date_now    = date( 'd/m/Y H:i:s', current_time( 'timestamp', 0 ) );
                $date_expire = get_field('expiration');

                $now     = new DateTime($date_now);
                $expires = new DateTime($date_expire);

                $diff = $expires->diff($now)->format("%a dias, %h horas e %i minutos");

                if ($now < $expires) {
                    echo "Expira em $diff", PHP_EOL;
                } 
                else if ($now >= $expires) {
                    echo "Expirado!", PHP_EOL;
                }
                else {
                    echo "", PHP_EOL;
                }
            ?>
    
asked by anonymous 12.02.2016 / 18:33

1 answer

2

Do so puts the day into a $ day variable. and check if it is 0 it does not show the day.

<?php
$date_now    = date('d/m/Y H:i:s', current_time('timestamp', 0));
$date_expire = get_field('expiration');

$now     = new DateTime($date_now);
$expires = new DateTime($date_expire);

$dia = $expires->diff($now)->format("%a");

if ($dia != 0) {
    $diff = $expires->diff($now)->format("%a dias, %h horas e %i minutos");
} else {
    $diff = $expires->diff($now)->format("%h horas e %i minutos");
}

if ($now < $expires) {
    echo "Expira em $diff", PHP_EOL;
} else if ($now >= $expires) {
    echo "Expirado!", PHP_EOL;
} else {
    echo "", PHP_EOL;
}
?>
    
12.02.2016 / 19:23