Home › Forums › General Issues › Date Time Picker Check
I want to display a certain callout on my site (it is an ACF image field), but I want it only to display during a certain time.
I am looking to use a Date Time Picker field for the start date/time and a Date Time Picker for the end date/time. However, how would I check to see if the current date/time is within the start and end time frame?
this is only one possibility
// current unix time stamp
$now = time();
// get field with no formatting
// this will return date time as stored in the db
// to that there isn't any problem with the date format
// when calling strtotime()
$start = strtotime(get_field('start_date_time', false, false));
// same for end
$end = strtotime(get_field('end_date_time', false, false));
if ($start <= $now && $now <= $end) {
// in the middle, do something.
}
Awesome.
And since I am actually using this in conjunction with a javascript function, would the below work?
<script>
var nowTime = <?php time(); ?>;
var startTime = <?php strtotime(get_field('start_date_time', false, false)); ?>;
var endTime = <?php strtotime(get_field('end_date_time', false, false)); ?>;
if (startTime <= nowTime && nowTime <= endTime) {
//do something
}
</script>
Also, I am assuming the PHP time(); function and the Date Time Picker both use the server’s time zone, correct?
You would need to echo the values to get them into the JS
<script>
var nowTime = <?php echo time(); ?>;
var startTime = <?php echo strtotime(get_field('start_date_time', false, false)); ?>;
var endTime = <?php echo strtotime(get_field('end_date_time', false, false)); ?>;
if (startTime <= nowTime && nowTime <= endTime) {
//do something
}
</script>
And I am assuming the time() function uses the server’s time zone, correct? So the Date Time Picker date/time selected should use that same time zone as the server, correct?
Yes it does, but I don’t know how ACF gets the current time, I don’t really think it needs to get the time because you use fields to set times. There is a function in wp to get the time https://codex.wordpress.org/Function_Reference/current_time, but in the end it’s just a complicated way to call time().
The topic ‘Date Time Picker Check’ 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.