将时间与PHP和WordPress进行比较

时间:2012-05-06 04:41:07

标签: php wordpress datetime

我试图创建一个比较一段信息时间的插件。在WordPress数据库中的当前时间。例如,如果帖子在5/6中午12:00过期,则在此之前它不会显示过期的标签。

日期和时间表达式采用以下格式:MM / DD / YYYY HH:MM(军事时间)。 这是迄今为止我对PHP代码所拥有的内容。有人可以查看我的代码,看看我做错了吗?请? :)

global $post;
$expired_post = get_post_meta( $post->ID, 'deal_exp_dt', true);
$expired_post_time = strtotime($expired_post);

// CONTENT FILTER
add_filter( 'the_content', 'my_the_content_filter', 20 );
function my_the_content_filter( $content ) {
if(time() > $expired_post_time && !empty($expired_post)) {
    $content = sprintf(
        '<div id="expired_deal_post"><strong><span style="font-size: x-large;">EXPIRED</span></strong><br><span style="font-size: medium;">This deal has ended.</span></div>%s',
        $content
    );

// Returns the content.
return $content;
}}

1 个答案:

答案 0 :(得分:1)

function my_the_content_filter( $content ) {

// these two variables should reside this function scope, otherwise declare them as global

$expired_post = get_post_meta( $post->ID, 'deal_exp_dt', true);
$expired_post_time = strtotime($expired_post);



if(time() > $expired_post_time && !empty($expired_post)) {
    $content = sprintf(
        '<div id="expired_deal_post"><strong><span style="font-size: x-large;">EXPIRED</span></strong><br><span style="font-size: medium;">This deal has ended.</span></div>%s',
        $content
    );
 }

// Returns the content.
return $content;
}