Mitchell
Mitchell

Reputation: 503

Comparing Times with PHP and WordPress

I'm attempting to create a plugin that compares the time of a piece of info. in the WordPress database to the current time. For example, if the post expires on 5/6 at 12:00pm, it wouldn't display the expired tag until that time.

The date and time expressions are in this format: MM/DD/YYYY HH:MM (in military time). Here's what I have for PHP code so far. Can someone look over my code and see if I am doing something wrong? Please? :)

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;
}}

Upvotes: 0

Views: 87

Answers (1)

thecodeparadox
thecodeparadox

Reputation: 87073

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;
}

Upvotes: 1

Related Questions