Support

Account

Home Forums Front-end Issues Date expiry issue

Solving

Date expiry issue

  • Hello,
    I search a lot and find the my answer that how I can show text after date expire and its good but the thing is its show the text on that date. Let say I give last date 15/9/2020 and when 15/9/2020 comes it shows text. Is it possible that when 16/9/2020 comes then it will show the text. Here is my code

    $currentdate = new DateTime();
    $date = get_field('last_date', false, false);
    $date = new DateTime($date);
    if ($currentdate > $date) {
    echo '(Job Expired)';  
    } else {
    echo get_field( 'last_date' );
    }
  • I’m not sure I understand what you are asking. Do you mean that you want to wait until a month has passed before showing “(Job Expired)”?

    
    $date = get_field('last_date', false, false);
    $date = new DateTime($date);
    $date->modify('+1 month');
    
  • No actually my code works like this.

    I have posted a job and given a last date, which is 10 Jan 2020, so when 10 Jan 2020 comes, it will show job Expired but actually I don’t want to show that text on that date, it will show on 11 Jan 2020.

  • I’m not too sure because I’m not completely sure how DateTime() works so this is an assumption.

    When you call

    
    $date = get_field('last_date', false, false);
    $date = new DateTime($date);
    

    there is not time so date/time is the value of the day at 00:00:00 (midnight)

    But when you call
    $currentdate = new DateTime(); this uses the default of “now” which has a time and because of the $currentdate will always be greater than $date except on the same day. Try setting $currentdate without a time formatted the same as what get_field() is returning.

    
    $currentdate = new DateTime(date('Ymd'));
    
  • I have added your line but now its not showing the text, which i have mentioned. and i am using this format of last_date field M d, Y

  • LIke I said, I wasn’t sure. But I’m still thinking that the date from the field is producing a date with a midnight time and current date time has the current time so it is greater than the other value.

    You need to echo out the actual values of each to see what you are working with and adjust your function calls so that you get values that will work.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.