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;
}
?>
It worked, thanks for your help, Tutijapa.
– Rafael