简体   繁体   English

如何以今天,明天,昨天的形式获取生日?

[英]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' 1.如果候选人生日是1991年7月26日,则应打印“昨天”

  • 2.if candidate birthday is on 27-july-1991 it should be print 'today'. 2.如果候选人生日是1991年7月27日,则应打印“今天”。

  • 3.if candidate birthday will on 28-july-1991 it should be print 'tomorrow'. 3.如果候选人生日是1991年7月28日,则应打印“明天”。

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 使用php Date和Time

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,您的代码就可以使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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