WordPress Shortcode to get today’s birthday, yesterday and the day before

this is a shortcode created for a client to get the birthday of his employee from a custom database table with 3 varchar fields : name, area and date.

you will notice there is two versions one using server time and the other using wordpress current time function. because the timezone of the server is different than the user set timezone in wordpress

// [birthday-today] shortcode
function birthday_today_func( $atts ){

global $wpdb;

    $birthdays = $wpdb->get_results("SELECT * FROM Birthdays WHERE MONTH(STR_TO_DATE(date, '%d/%m/%Y')) = MONTH(NOW()) AND DAY(STR_TO_DATE(date, '%d/%m/%Y')) = DAY(NOW())");

    if($birthdays){
		
		$birthdays_out =  "Today's birthdays:<br><ul>";
		foreach ( $birthdays as $birthday ) 
		{
			$birthdays_out .= '<li> '.$birthday->name. ' - '.$birthday->area. ' - '.$birthday->date.'.</li>';
		}

		$birthdays_out .= '</ul>';

	}

if(date('D', $timestamp) === 'Mon') {

	$y_birthdays = $wpdb->get_results("SELECT * FROM Birthdays WHERE MONTH(STR_TO_DATE(date, '%d/%m/%Y')) = MONTH(CAST(NOW() - INTERVAL 1 DAY AS DATE)) AND DAY(STR_TO_DATE(date, '%d/%m/%Y')) = DAY(CAST(NOW() - INTERVAL 1 DAY AS DATE))");

	if($y_birthdays){
		$birthdays_out .= '<br>  Birthdays on '.date("l  d/m", strtotime("yesterday")).'<br><ul>';

		foreach ( $y_birthdays as $y_birthday ) 
		{
			$birthdays_out .= '<li> '.$y_birthday->name. ' - '.$y_birthday->area. ' - '.$y_birthday->date.'.</li>';
		}

		$birthdays_out .= '</ul>';
	}




	$y2_birthdays = $wpdb->get_results("SELECT * FROM Birthdays WHERE MONTH(STR_TO_DATE(date, '%d/%m/%Y')) = MONTH(CAST(NOW() - INTERVAL 2 DAY AS DATE)) AND DAY(STR_TO_DATE(date, '%d/%m/%Y')) = DAY(CAST(NOW() - INTERVAL 2 DAY AS DATE))");

	if($y2_birthdays){
		$birthdays_out .= '<br>Birthdays on '.date("l d/m", strtotime("-2 day")).'<br><ul>';
		foreach ( $y2_birthdays as $y2_birthday ) 
		{
			$birthdays_out .= '<li> '.$y2_birthday->name. ' - '.$y2_birthday->area. ' - '.$y2_birthday->date.'.</li>';
		}

		$birthdays_out .= '</ul>';
	}
}

return $birthdays_out;
}
add_shortcode( 'birthday-today', 'birthday_today_func' );
// [birthday-today] shortcode
function birthday_today_func( $atts ){

global $wpdb;
$current_time =current_time( 'mysql' );
$sql = "SELECT * FROM Birthdays WHERE MONTH(STR_TO_DATE(date, '%d/%m/%Y')) = MONTH('".$current_time."') AND DAY(STR_TO_DATE(date, '%d/%m/%Y')) = DAY('".$current_time."')";
    $birthdays = $wpdb->get_results($sql);

    if($birthdays){
		//print_r($birthdays);
		$birthdays_out =  "Hoy cumplen años:<br><ul>";
		foreach ( $birthdays as $birthday ) 
		{
			//$birthdays_out .= '<li> '.$birthday->name. ' - '.$birthday->area. ' - ' .$birthday->date. '.</li>';
			$birthdays_out .= '<li> '.$birthday->name. ' - '.$birthday->area.'.</li>';
		}

		$birthdays_out .= '</ul>';

	}
	
	
if(current_time('D') === 'Mon') {
$sql2 = "SELECT * FROM Birthdays WHERE MONTH(STR_TO_DATE(date, '%d/%m/%Y')) = MONTH(CAST('".$current_time."' - INTERVAL 1 DAY AS DATE)) AND DAY(STR_TO_DATE(date, '%d/%m/%Y')) = DAY(CAST('".$current_time."' - INTERVAL 1 DAY AS DATE))";
	$y_birthdays = $wpdb->get_results($sql2);

	if($y_birthdays){
		$yest = $current_time.' -1 day';
		$birthdays_out .= '<br>  Cumplieron el domingo '.date("l  d/m", strtotime($yest)).'<br><ul>';

		foreach ( $y_birthdays as $y_birthday ) 
		{
			//$birthdays_out .= '<li> '.$y_birthday->name. ' - '.$y_birthday->area. ' - ' .$y_birthday->date.  '.</li>';
			$birthdays_out .= '<li> '.$y_birthday->name. ' - '.$y_birthday->area.'.</li>';
		}

		$birthdays_out .= '</ul>';
	}



$sql3 = "SELECT * FROM Birthdays WHERE MONTH(STR_TO_DATE(date, '%d/%m/%Y')) = MONTH(CAST('".$current_time."' - INTERVAL 2 DAY AS DATE)) AND DAY(STR_TO_DATE(date, '%d/%m/%Y')) = DAY(CAST('".$current_time."' - INTERVAL 2 DAY AS DATE))";
	$y2_birthdays = $wpdb->get_results($sql3);

	if($y2_birthdays){
		$day_before_yest = $current_time.' -2 days';
		$birthdays_out .= '<br>Cumplieron el sabado '.date("l d/m", strtotime($day_before_yest)).'<br><ul>';
		foreach ( $y2_birthdays as $y2_birthday ) 
		{
			//$birthdays_out .= '<li> '.$y2_birthday->name. ' - '.$y2_birthday->area. ' - ' .$y2_birthday->date.  '.</li>';
			$birthdays_out .= '<li> '.$y2_birthday->name. ' - '.$y2_birthday->area.'.</li>';
		}

		$birthdays_out .= '</ul>';
	}
}

return $birthdays_out;
}
add_shortcode( 'birthday-today', 'birthday_today_func' );

 

the result:

PSA Peugeot Citroën 2014-09-30 03-49-11

 

used with this plugin to import csv into the database table:
https://www.tipsandtricks-hq.com/wp-csv-to-database-plugin-import-excel-file-content-into-wordpress-database-2116

Leave a Reply

Your email address will not be published. Required fields are marked *