Get today's date new / modified user and data update to klaviyo

If you want to get WordPress users that falls into a specific date or today date then simply wrote this function into functions.php file.

        $today_date = date("F dS, Y");
	$today_timestamp = strtotime($today_date);

	$users = array();

	global $wpdb;

	$user_register_results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}users WHERE user_registered BETWEEN '".date("Y-m-d")."' AND '".date('Y-m-d', strtotime(' +1 day'))."'", OBJECT );
	if ($user_register_results) {
		foreach ($user_register_results as $user_register_result) {
			$users[] = $user_register_result->ID;
		}
	}

	$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}usermeta WHERE meta_key = 'last_update'", OBJECT );
	if ($results) {
		foreach ($results as $result) {
			$user_timestamp = $result->meta_value;
			$user_last_timestamp = strtotime( date('F dS, Y', $user_timestamp) );

			if( $user_last_timestamp == $today_timestamp ) {
				$users[] = $result->user_id;
			}
		}
	}

	var_dump($users);

 

Moreover, if you achieve this functionality using Cron job and also store data into Klaviyo CRM then just paste this below code into functions.php file.

 
add_filter( 'cron_schedules', 'dandd_add_every_one_minute' );
function dandd_add_every_one_minute( $schedules ) {
    $schedules['every_one_minute'] = array(
            'interval'  => 60,
            'display'   => __( 'Every 1 Minute', 'textdomain' )
    );
    return $schedules;
}

if ( ! wp_next_scheduled( 'dandd_add_every_one_minute' ) ) {
	wp_schedule_event( time(), 'every_one_minute', 'dandd_add_every_one_minute' );
}

add_action( 'dandd_add_every_one_minute', 'every_one_minute_event_func' );
function every_one_minute_event_func() {
    
    $today_date = date("F dS, Y");
	$today_timestamp = strtotime($today_date);

	$users = array();

	global $wpdb;

	$user_register_results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}users WHERE user_registered BETWEEN '".date("Y-m-d")."' AND '".date('Y-m-d', strtotime(' +1 day'))."'", OBJECT );
	if ($user_register_results) {
		foreach ($user_register_results as $user_register_result) {
			$users[] = $user_register_result->ID;
		}
	}

	$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}usermeta WHERE meta_key = 'last_update'", OBJECT );
	if ($results) {
		foreach ($results as $result) {
			$user_timestamp = $result->meta_value;
			$user_last_timestamp = strtotime( date('F dS, Y', $user_timestamp) );

			if( $user_last_timestamp == $today_timestamp ) {
				$users[] = $result->user_id;
			}
		}
	}

	foreach ($users as $user) {
		$user_id = $user;
		$userObj = get_user_by('id', $user_id);
		$userEmail = $userObj->user_email;

		$api_key = 'pk_e827647587e4cbf1c00f3bf137d7f4628b';
		$token = '03df25c845d460bcdad7802d2vf6fc1dfde97283bf75cc993eb6dca835ea2e2r';

		$user_brands = get_user_meta( $user_id, 'brands_your_options' );
		$user_other_brands = get_user_meta( $user_id, 'brands_other_options' ); 
		$salon_name = get_user_meta( $user_id, 'salon_name' );

		$update_body = array(
			"profiles" => [ 
				array(
					'email' => $userEmail,
					'push_tokens' => "03df25c845d460bcdad7802d2vf6fc1dfde97283bf75cc993eb6dca835ea2e2r",
					'Brands Carried' => $user_brands,
					'Other Brands Carried' => $user_other_brands,
					'Saloon Name' => $salon_name
				)
			]
		);

		$response = wp_remote_post( 'https://a.klaviyo.com/api/v2/list/XKidAA/members?api_key=' . $api_key, [
			'headers'     => array('Content-Type' => 'application/json; charset=utf-8'),
			'body'        => json_encode($update_body),
			'data_format' => 'body',
			] 
		);
	}
}