Search code examples
wordpressadvanced-custom-fieldsuser-profile

Wordpress list users who has their birthday today


There are more than 800 user profiles in my system and there is an extra field in the user profiles to enter their birthday. (Created with ACF) I need to display the names of the users who have their birthday today.

I tried the following query several times but I was unable to match the date with todays date because of the year.

<?php
$today = date("M d y");
$blogusers = get_users(array(
    'role'      => 'subscriber',
    'meta_key'  => 'date_of_birth',
    'orderby'   => 'meta_value',
    'order'     => 'DESC',
));

foreach($blogusers as $user){
$name = $user->first_name;
$date = $user->date_of_birth;
$date = new DateTime($date);

?>

    <div class="person">
        <div class="col-sm-8"><?php echo $name; ?></div>
        <div class="col-sm-4 text-right"><?php echo $date->format('M d y'); ?></div>
        <div class="clear"></div>
    </div>


<?php } ?>

Solution

  • <?php
    $today = date("M d");
    $blogusers = get_users(array(
        'role'      => 'subscriber',
        'meta_key'  => 'date_of_birth',
        'orderby'   => 'meta_value',
        'order'     => 'DESC',
    ));
    
    foreach($blogusers as $user){
    $name = $user->first_name;
    $date = $user->date_of_birth;
    $date = date("M d",strtotime($date));
    if($today == $date){
    ?>
    
        <div class="person">
            <div class="col-sm-8"><?php echo $name; ?></div>
            <div class="col-sm-4 text-right"><?php echo $date; ?></div>
            <div class="clear"></div>
        </div>
    
    
    <?php 
        }
      } 
    ?>