Jump to content
the_toolman

Display number of days between a date and today's date

Recommended Posts

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

Share this post


Link to post
Share on other sites

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

  • Like 1

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.