Jayesh Shrimali Jayesh Shrimali - 1 month ago 12
PHP Question

How to display birthday as yesterday, today and Tomorrow in php (Year doesn't matter)

How to get birthday as yesterday, today and tomorrow irrespective of year in php. For example I have tried.

<?php
$datss = '11-10-1992';
$yourDate = new DateTime($datss);
$yourDate->setTime(0, 0, 0);

$now = new DateTime();
$now->setTime(0, 0, 0);

$diff = $now->diff($yourDate);
$diffInDay = (int)$diff->format('%R%d%m');

if ($diffInDay == -1) {
echo 'yesterday ';
}
else if ($diffInDay == 0) {
echo "it's my birthday.";
}
else if ($diffInDay == +1) {
echo 'Tomorrow';
}

else{
echo '11-10-1992';
}
?>


It is echoing
"it's my birthday."
correctly but when given
$datss = '12-10-1992';
it is not showing
'Tomorrow'

Answer

Please try the below given code

<?php

$yourDate = '12-10-1992';
$now =  'now';

$yourDate = date('d-m-1970',strtotime($yourDate));
$now = date('d-m-1970',strtotime($now));

$diff = strtotime($yourDate) - strtotime($now);
$diffInDay= floor($diff/(60*60*24));    


if ($diffInDay == -1) {
    echo 'yesterday ';
}
else if ($diffInDay == 0) {
    echo "it's my birthday.";
}
else if ($diffInDay == +1) {
    echo 'Tomorrow';
}

else{
    echo '11-10-1992';
}

?>

Now if you give the date as '12-10-1992' it will print 'Tommorow' and if '10-10-1992' is given ,this will print as 'yesterday'.Also the year will not matter anymore ie if you give date as '12-10-1993',it will still print as 'Tommorow'.