0

I need to showing message in post , but after one month from publish date.

I try to make it before ask you, but I can't

this is my code

                $publish_date =  $post->post_date_gmt;
        $today = date('d-m-Y h:i:s');
        $nextMonth = mktime(0 , $publish_date('m')+1 , 0 , 0 , 0,0);
        echo 'Publish Date:'.$publish_date.'<br/>';
        echo 'Today Date:'.$today.'<br/>';
        echo 'Next Month:'.$nextMonth;
        if($today => $nextMonth){
            echo 'One Month or more for publish post';
        } else {
            echo '';
        }

2 Answers 2

1

Custom conditional I wrote for my theme (mostly to kick some widgets and ads from newer posts), works nicely:

function is_old_post( $days = 14 ) {

    if( !is_single() )
       return false;

    $id = get_the_ID();
    $date = get_post_field( 'post_date', $id );

    if( strtotime( $date ) < strtotime( "-{$days} days" ) )
        return true;

   return false;
}
0
1

Couple of things:

  1. Your today date should have the format 'Y-m-d h:i:s'.
  2. You might want to use current_time('mysql') instead, anyways (WP core function).
  3. $publish_date('m')?, and mktime creates a unix timestamp, afaik, not a mysql one, which is what you're after, so instead of those, try this:

    $ts = strtotime($today);
    $m = date('m', $ts);
    $Y = date('Y', $ts);
    if ($m < 12) { $m++; } else { $m = 1; $Y++; }
    $nextMonth = date('Y-m-d', mktime( 0, 0, 0, $m, date('d', $ts), $Y ));
    
0

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.