the_toolman Posted July 6, 2019 Share Posted July 6, 2019 Hi, I have a job listing website which displays the closing date of applications using: $expired_date (This displays a date such as 31st December 2019) I am trying to show a countdown/number of days left until the closing date. I have put this together, but I can't get it to show the number of days. <?php $expired_date = get_post_meta( $post->ID, '_job_expires', true ); $hide_expiration = get_post_meta( $post->ID, '_hide_expiration', true ); if(empty($hide_expiration )) { if(!empty($expired_date)) { ?> <span><?php echo date_i18n( get_option( 'date_format' ), strtotime( get_post_meta( $post->ID, '_job_expires', true ) ) ) ?></span> <?php $datetime1 = new DateTime($expired_date); $datetime2 = date('d'); $interval = $datetime1->diff($datetime2); echo $interval->d; ?> <?php } } ?> Can anyone help me with what I have wrong? Many thanks Quote Link to comment Share on other sites More sharing options...
Barand Posted July 6, 2019 Share Posted July 6, 2019 consider <?php $dt1 = new DateTime(); $dt2 = new DateTime('2019-12-31'); echo $dt1->diff($dt2)->d . '<br>'; //--> 24 echo $dt1->diff($dt2)->days . '<br>'; //--> 177 ?> "d" gives days as in 6 months 24 days. "days" gives the total number of days 1 Quote Link to comment Share on other sites More sharing options...
the_toolman Posted July 6, 2019 Author Share Posted July 6, 2019 Thank you very much I modified it to show just the days with the $expired_date variable: Quote <?php $dt1 = new DateTime(); $dt2 = new DateTime($expired_date); echo $dt1->diff($dt2)->days; ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.