Home Forums ACF PRO Get date or time from the Date & Time Picker field


Get date or time from the Date & Time Picker field

  • Hi

    Is it at all possible to grab either the date and/or the time from the Date & Time Picker field?

    I need this for two reasons:
    1) To echo one or either (date or time) on different page templates
    2) To run a custom WP_Query, and list custom posts by date and then time.

    Many thanks

  • Hi @huwrowlands

    The best way to get only the date or the time is by converting the value to a date object like this:

    $date = get_field('date_time_picker', 85, false);
    $date = DateTime::createFromFormat('Y-m-d H:i:s', $date);
    echo $date->format('Y-m-d');
    echo "\n";
    echo $date->format('H:i:s');

    Please check this page to see the available format:

    I’m afraid you need to query it as whole date and time. If you need to query it separately, you need to create separate fields for the date and the time.

    I hope this makes sense 🙂

  • @james

    I think I’ve got a solution and will post my findings here if I do!
    Two fields: Date (datepicker) and start time (time picker). Sort by date and then orderby date and time.

    Thanks for the help.

  • I managed to solve my problem using the answer here:

    			$args = array(
    			  'post_type'      => 'schedule',
    			  'posts_per_page' => '-1',
    			  'meta_query'     => array(
    			      'relation'   => 'AND',
    			        'date_clause' => array(
    			            'key'     => 'schedule_time_slot_%_schedule_show_date',
    			            'compare' => '=',
    			            'value'   => $tue,
    			        'time_clause' => array(
    			            'key'     => 'schedule_time_slot_%_schedule_show_start_time',
    			            'compare' => 'EXISTS',
    			    'orderby' => array(
    			        'date_clause' => 'ASC',
    			        'time_clause' => 'ASC',
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.