0

I am trying to compare dates from a custom field. I have them stored as something like "20121125". If the date has expired, a placeholder image will be used, if not, the default thumbnail will be used. Any advice is appreciated.

<?php
$html = "";
$key = get_post_meta($post->ID, 'Due', true);
$date = date('ymd');
if ($key >= $date) {
     the_post_thumbnail('medium');
} else {
    $html .= '<img src="http://exampleimage.com" alt="">';
}
return $html;
?>

3 Answers 3

1

Well, the exact server time/timestamp can be retrieve with current_time function. And comparing your date format, it would need to be Ymd. So combining these two thing, the proper comparison for you should be -

$key = get_post_meta( $post->ID, 'Due', true );
$date = date('Ymd', current_time('timestamp') );

if( $key >= $date )
{
    // note to retrive the post thumbnail, you need to use get_the_post_thumbnail,
    // as the_post_thumbnail function actually echo the output
    $html .= get_the_post_thumbnail(null, 'medium');
}
else 
{
    $html .= '<img src="http://exampleimage.com" alt="">';
}
return $html;

Now, there could be one question in which timezone you are saving the date in the post meta. If the value is sanitized with the server time (as the post_date value is saved), then this is perfect comparison.

4
  • This looks great, but I think I have something wrong with my return $html; ... all the content is pushed to the left on my site and other info is just no there. Any idea? :)
    – Jagst3r15
    Dec 21, 2013 at 15:12
  • 1
    Well, without seeing you complete code, and where you are actually using it, it's hard to tell. If you are using this within a function, then it should be fine for each loop, but if u are using this within the template file, then there shouldn't be 'return $html' as this will stop all the next PHP processing and templates ex footer/sidebar. Can you be a little more specific ?
    – Shazzad
    Dec 21, 2013 at 15:35
  • Yes that is exactly what I did, I have it in a template file and the next bit of php is not parsing :( ..... see hastebin.com/pisonufoje.php
    – Jagst3r15
    Dec 21, 2013 at 15:52
  • 1
    You need to echo it actually, instead of return $html; , use echo $html;
    – Shazzad
    Dec 21, 2013 at 18:32
0

You can try this:

<?php
$html = "";
$key = get_post_meta($post->ID, 'Due', true);
date_default_timezone_set('Africa/Lagos'); //Only if needed. Change this for your timezone
if (strtotime($key) >= time()) {
     the_post_thumbnail('medium');
} else {
    $html .= '<img src="http://exampleimage.com" alt="">';
}
return $html;
?>

Here we are using the strtotime function to help us compare these two dates.

You can find more information here:

Hope it helps.

0

You can store the date after coonverting to timestamp using custom fields(i.e. in post_meta table). By doing so you will be able to compare dates easily. Then you can use your code as follows:

<?php
$html = "";
$stored_timestamp = get_post_meta($post->ID, 'Due', true);
$current_timestamp = strtotime(date('Y-m-d'));
if ($stored_timestamp >= $current_timestamp) {
    the_post_thumbnail('medium');
} else {
    $html .= '<img src="http://exampleimage.com" alt="">';
}
return $html;
?>

Store timestamp as:

update_post_meta($post->ID,'Due',strtotime(date('Y-m-d'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.