0

It's really hilarious but my the_date() and the_time() functions show the current date and time instead of the post's publication time, therefore at each refresh of the page, the time changes according to the actual time and date. I am using these functions in a single template for a custom post in the loop, like so:

<?php if(have_posts()): ?>
    <?php while(have_posts()): the_post(); ?>
       <h3> <?="Titre du projet"?> </h3>
                <?php the_title(); ?>
       <h3><?="Description"?></h3>
                <?php the_content(); ?>
    <p> <?php the_date(); ?> à <?php the_time(); ?>
    <?php endwhile?>
<?php endif;?>

What's wrong? I tried other similar functions or get post date in the $post object, but in their format, the date and time are inseparable. and I need them separately. Thanks.

2
  • 1
    are you updating the post in frontend requests? And if the posts publish date is not now, what does it say it is in admin? What's the difference between the two? Seconds? Minutes? Hours? Weeks? Is it changing on admin after viewing on the frontend? does disabling all plugins fix the issue? Have you tried turning them all off then turning them on one by one until the problem returns?
    – Tom J Nowell
    Commented Sep 19, 2021 at 20:07
  • Is the date being updated in the database every time the page loads? If so, the issue is not the functions, it is code somewhere else thats updating the post. I can see from your post history that you are implementing front end submissions, so it seems very likely there's something wrong with your code that does that. Commented Sep 20, 2021 at 6:05

2 Answers 2

0

the_date(): By default, it will echo the date of the post in the format F j, Y, so if the post was published on 20 November 2018, it would echo November 20, 2018. get_the_date(): This fetches the date and doesn't echo it out. To echo it, you'd use echo get_the_date(), which gives you the same result as the_date(). It's useful if you're already using echo in your code. It can also help you get round the problem of dates not being displayed, as you'll see shortly.

You can also use get post id and using that id you can get post date and time


Reference https://code.tutsplus.com/tutorials/displaying-the-date-and-time-in-the-loop--cms-32237

echo get_the_date( get_post_ID());

<ul class="newsletters">
    <?php $query = new WP_Query( array(
        'post_type' => 'post',
        'category_name' => 'newsletters',
        'posts_per_page' => 8
    ) );
    while ($query->have_posts()) : $query->the_post(); ?>
    <li class="home newsletter"><a href="<?php the_permalink(); ?>"><?php the_title(); ?> - <?php echo get_the_date( 'j F, Y' ); ?></a></li>
    <?php endwhile; ?>
</ul>
0

So, this is what I finally think: the_date() and the_time() purpose is to display published date and time, even if in the documentation they said they display or retrieve the date/time the current post was written.
my error comes from the fact that I expected to have the date of writing (draft) in the absence of the date of publication;
And unfortunately all my posts were drafts, so I'm guessing that because the posts weren't published, the functions return the current date/time. And as soon as they were published, the date & time have stopped to the time they have been published.

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.