简体   繁体   中英

How to get birthday date in form of today, tomorrow, yesterday?

I want to get birthday dates in form of 'Today' ,'Tomorrow','Yesterday' form.

  • 1.if candidate birthday was on 26-july-1991 it should be print 'yesterday'

  • 2.if candidate birthday is on 27-july-1991 it should be print 'today'.

  • 3.if candidate birthday will on 28-july-1991 it should be print 'tomorrow'.

code

 $current = strtotime(date("Y-m-d"));
 $date    = strtotime("2014-07-24");

 $datediff = $date - $current;
 $difference = floor($datediff/(60*60*24*365));
 if($difference==0)
 {
 echo 'today';
 }
 else if($difference > 1)
 {
 echo 'Future Date';
 }
 else if($difference > 0)
 {
 echo 'tomarrow';
 }
 else if($difference < -1)
 {
 echo 'Long Back';
 }
else
{
echo 'yesterday';
}  

Maybe a bit complicated solution, but here I compare month num and date num:

$current_month = date("n");
$current_day = date("j");

// date of birth
$dob = strtotime("1991-07-26");
$dob_month = date("n", $dob);
$dob_day = date("j", $dob);

if ($current_month == $dob_month) {
    if ($current_day == $dob_day) {
        echo 'TODAY';
    } elseif ($current_day == $dob_day + 1) {
        echo 'YESTERDAY';
    } elseif($current_day == $dob_day - 1) {
        echo 'TOMORROW';
    } else {
        echo 'IN this month';
    }
} elseif ($current_month < $dob_month) {
    echo 'In future';
} else {
    echo 'Long back';
}

Use the php Date and Time class

Something like this:

$today=new DateTime("2017-07-27");
$other_day=new DateTime("2017-07-28");

$check = $today->diff($other_day);

$difference = (integer)$check->format( "%R%a" );
echo $difference;

只需删除* 365,您的代码就可以使用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM