简体   繁体   English

不要在Wordpress帖子上显示过期日期信息

[英]Do not show expiration date information on Wordpress posts

I am using the code below to report the time it takes for a post expires in my Wordpress site, the value of expiration I define in a Custom Field (expiration). 我正在使用下面的代码报告我的Wordpress网站中帖子过期所花费的时间,即我在“自定义字段”中定义的过期值(过期)。

From this code, as I do I keep a post that will expire in about four hours, does not display the information in this way: "Expires in 0 days , 4 hours and 49 minutes." 按照我的做法,我保留了这段代码,该消息将在大约四个小时后过期,而不会以这种方式显示信息:“在0天,4小时49分钟后过期”。

I do not want the "0 days" is displayed, only the remaining hours. 我不希望显示“ 0天”,而仅显示剩余时间。 In the case of the post expires the next day, so good, could be displayed normally "Expires in 1 day, 4 hours and 49 minutes." 如果该帖子在第二天过期,那么可以正常显示为“在1天4小时49分钟后过期”。

In addition, there are posts that I do not define expiration, and they all appear the following information: "Expires in 0 days , 2 hours and 0 minutes." 另外,有些帖子我没有定义过期时间,并且它们都显示以下信息:“ 0天,2小时零分钟内过期”。 How do I display information like "Undetermined Expiration" for these posts? 我如何显示这些帖子的信息,例如“不确定的过期时间”?

           <?php 
                $date_now    = date( 'm/d/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 days, %h hours and %i minutes");

                if ($now < $expires) {
                    echo "Expires em $diff", PHP_EOL;
                } 
                else if ($now >= $expires) {
                    echo "Expired!", PHP_EOL;
                }
                else {
                    echo "", PHP_EOL;
                }
            ?>

Use this code: 使用此代码:

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

$diff = $expires->diff($now);

if( $diff->days ) $diff = $diff->format( "%a days, %h hours and %i minutes" );
else              $diff = $diff->format( "%h hours and %i minutes" );

->days is the only absolute DateTime property. ->days是唯一的绝对DateTime属性。

If you want display only minutes when the difference is lower than 1 hour, you can change above code in this way: 如果您希望仅显示差值小于1小时的分钟数,则可以通过以下方式更改上面的代码:

if( $diff->days  ) $diff = $diff->format( "%a days, %h hours and %i minutes" );
elseif( $diff->h ) $diff = $diff->format( "%h hours and %i minutes" );
else               $diff = $diff->format( "%i minutes" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM