How to use php to check age for birthday messages

How to use php to check age for birthday messages
How to use php to check age for birthday messages

We going to check age for birthday messages using the same function we’ve used earlier in How to validate date of birth

we are simply going to make a little change to it.

the we added a logic that checks if current day and month matches with the ones in the given date of birth

if so then the wish do whatsoever the developer defines

 
<?php 
function birthday_wish($date){
	$dateparameters = array();
	$date = preg_replace("/[^0-9\']/", ",", $date);	
	$date = explode(',', $date);
	$wish = '';
	if(count($date) == 3){
    for ($i = 0; $i < 3; ++$i) {
        $dateparameters []= $date[$i];
    }
	
	$year = $dateparameters[0];
	$month = $dateparameters[1];
	$actualmonth = $dateparameters[1];
	$actualday = $dateparameters[2];
	$actualdate = $year.'-'.$month.'-'.$actualday;
	$day = cal_days_in_month(CAL_GREGORIAN, $month, $year);

	$day = range(1, $day); // day range
	$month = range(1, 12); // month range
	
	if(in_array($actualday, $day) && in_array($actualmonth, $month)){ //Check if actual month and day is in range of days that should be in the provided month of the given year
		//THE BIRTHDAY WISH
		if(date('m-d') == ($actualmonth.'-'.$actualday)){
			$wish = 1;
		}
	}
		
	}
return $wish;
}
?>

how to apply it

let’s apply the above function output in our jobs
it’s just by building a simple logic as shown below

 <?php
$date= '2010/09/21';
if(birthday_wish($date) == 1){
	echo 'Happy birthday to you: <br>You were born on this day '.date('l', strtotime(date('Y-m-d'))).', '.date('dS').' '.date('F', strtotime($date)).' being '.date('l', strtotime($date)).' of same month in the year '.explode('/',$date)[0];
}else{
	echo 'Hey: <br>You are not born on this day '.date('l', strtotime(date('Y-m-d'))).', '.date('dS').' '.date('F', strtotime($date)).' '.explode('/',$date)[0];
}
?>

Related

How TO create a Login Form (w3schools vs bootstrap)

Every transactional website should have a login form that...

Best PHP checkdate alternative: How to do date validation in php (exclusive)

this checkdate alternative special function validates date in such...

How to validate date of birth today & get the age from the birthday using php

we have already explained how you can get age...

How to check product expiration duration of any product using php (2 proven function)

with this simple php trick you can check expiration...

the 2 easy ways to create dynamic form inputs in php

these are 2 ways you can create dynamic form...