Home › Forums › Front-end Issues › Is my date in future ?
Hello there!
In a custom post I specify a date thanks to ACF.
I need to compare that field to the current date. If that field is in future I want to show “Soon”.
That is my current code :
<?php
$today = date("Y-m-d");
echo $today;
$date_fr = get_field('date_de_sortie_fr', false, false);
$date_fr = new DateTime($date_fr);
?>
<span><?php echo $date_fr->format('Y-m-d'); ?></span>
I think that my today date is not in a good format to do the comparison. Do you confirm ?
Also, how to compare that dates ? Can you help me please ?
Try this, using DateTime()
rather than date()
:
<?php
$today = new DateTime();
$date_fr = DateTime::createFromFormat('Ymd', get_field('date_de_sortie_fr'));
?>
<span>
<?php
if($date_fr > $today) echo "Soon: ";
echo $date_fr->format('Y-m-d');
?>
</span>
NOTE This requires that you store the date field in Ymd format.
This should work:
<?php
$endOfDay = strtotime("tomorrow") - 1;
$unixtimestamp = strtotime(get_field('date_de_sortie_fr'));
$date_fr = date("Y-m-d", $unixtimestamp);
echo $unixtimestamp > $endOfDay ? "soon" : $date_fr;
EDITED: Syntax Error
Thank you!!
The first solution solved my issue.
Just for information the second didn’t…
Oh, did you see the missing ;
at the end of $date_fr = date("Y-m-d", $unixtimestamp)
? :D… sorry. If you´re working with Date Picker field, this should also works fine :).
I didn’t, but i’ll try to confirm your code 😉.
Thank you again!
The topic ‘Is my date in future ?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.