Home Forums General Issues Show upcoming birthdays using a Date Picker field


Show upcoming birthdays using a Date Picker field

  • Hi,

    I have created a custom post type employees and I have added a date picker field called birthday to this post type. The date picker field is set to return the date as ‘Ymd’

    I would like to show on the website the upcoming birthdays, so it would list the employees whose birthdays fall within the next month.

    I have followed this thread:

    And this is the code I am using:

    global $post;
    //Set server timezone to central
    //Today's date
     $date_1 = date('md', strtotime("now")); 
      //Future date - the arg will look between today's date and this future date to see if the post fall within the 2 dates.
     $date_2 = date('md', strtotime("+1 month"));
      // returns unformatted date
      $date_birthday = get_field('birthday', false, false);
      // convert it to a time
      $birthday = strtotime($date_birthday);
    $args = array(
            'post_type'       => 'employees',       
            'posts_per_page'  =>  -1,    
            'meta_key'        =>  $birthday,
            'meta_compare'    => 'BETWEEN',
            'meta_type'       => 'numeric',
            'meta_value'      =>  array($date_1, $date_2),
            'orderby'         => 'meta_value_num',
            'order'           => 'ASC'
           $posts = get_posts($args);
         foreach($posts as $post) {
                       // the loop

    (I am getting only the day and the month of the date, as the year in this case would not work in the comparison)

    This is working fine for this month (september). The problem would be in december, as in this month $date_2 would look like 13XX and the birthdays from January will not show.

    How I could achieve this? I suppose there must be a better way to accomplish this, but I have looked through several threads in this forum, and this is what I have achieved so far.

    Any help would be very apreciatted.

    Thanks in advance,

  • You cannot do a between query using just the month and day on a date field. A date field stores values as ‘YYYYMMDD’. There is no way that I know of to query between to values that only include the month and day. You might be able to query these by using a REGEX value query, but this would not work to get values between two months.

    The only way to do this would be to have a field that only contains just the month and day.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.