Delete expiration date information in Wordpress posts

Asked

Viewed 67 times

2

I am using the code below to inform the time it takes for a post to expire on my Wordpress site, whose expiration value I define in a Custom Field (expiration).

From this code, as I prevent a post that will expire in 4 hours, do not display the information this way: "Expires in 0 days, 4 hours and 49 minutes".

I don’t want the "0 days" shown, just the remaining hours. In case the post expires the next day, then fine, it could be displayed normally "Expires in 1 days, 4 hours and 49 minutes".

In addition, there are posts that I do not set expiration, and in all of them the following information appears: "Expires in 0 days, 2 hours and 0 minutes". How do I display information like "Indeterminate 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;
                }
            ?>

1 answer

2


It does so puts the day in a variable $day. and checks 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;
}
?>
  • 1

    It worked, thanks for your help, Tutijapa.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.