与PHP和WordPress比较时间

与PHP和WordPress比较时间,php,wordpress,datetime,Php,Wordpress,Datetime,我正在尝试创建一个插件来比较一条信息的时间。在WordPress数据库中保存到当前时间。例如,如果post在5月6日下午12:00过期,则在该时间之前不会显示过期的标签 日期和时间表达式的格式为:MM/DD/YYYY HH:MM(军用时间)。 以下是我到目前为止对PHP代码的了解。有人能检查一下我的代码,看看我是否做错了什么吗?请?:) global$post; $expired\u post=get\u post\u meta($post->ID,'deal\u exp\u dt',true)

我正在尝试创建一个插件来比较一条信息的时间。在WordPress数据库中保存到当前时间。例如,如果post在5月6日下午12:00过期,则在该时间之前不会显示过期的标签

日期和时间表达式的格式为:MM/DD/YYYY HH:MM(军用时间)。 以下是我到目前为止对PHP代码的了解。有人能检查一下我的代码,看看我是否做错了什么吗?请?:)

global$post;
$expired\u post=get\u post\u meta($post->ID,'deal\u exp\u dt',true);
$expired\u post\u time=strottime($expired\u post);
//内容过滤器
添加内容过滤器(“内容过滤器”,“我的内容过滤器”,20);
函数“我的内容”过滤器($content){
如果(time()>$expired\u post\u time&&!empty($expired\u post)){
$content=sprintf(
“已过期
此交易已结束。%s”, $content ); //返回内容。 返回$content; }}
功能我的内容过滤器($content){
//这两个变量应该驻留在这个函数范围内,否则将它们声明为全局变量
$expired\u post=get\u post\u meta($post->ID,'deal\u exp\u dt',true);
$expired\u post\u time=strottime($expired\u post);
如果(time()>$expired\u post\u time&&!empty($expired\u post)){
$content=sprintf(
“已过期
此交易已结束。%s”, $content ); } //返回内容。 返回$content; }
您的确切问题是什么?您在函数之外有$expired\u post\u时间,这在典型的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;
}}
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;
}