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

时间:2017-07-27 10:13:11

标签: php

我希望以'今天','明天','昨天'的形式获得生日日期。

  • 1.如果候选人的生日是在1991年7月26日那么应该打印'昨天'

  • 2.如果候选人的生日是在1991年7月27日那么应该打印“今天”。

  • 3.如果候选人生日将于1991年7月28日生日,那么应该打印“明天”。

 $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';
}  

3 个答案:

答案 0 :(得分:5)

也许有点复杂的解决方案,但在这里我比较月数和日期数:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
	<div class="row">
		<h1>Site Name</h1>
		<div class="navbar navbar-default">
			<div class="container">
				<div class="navbar-header">
					<button class=" btn btn-primary navbar-toggle" data-toggle="collapse" data-target="#responsive-menu">
						<span class="sr-only">Open Navigation</span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
						<span class="icon-bar"></span>
					</button>
				</div>
				<div class="collapse navbar-collapse" id="responsive-menu">
					<ul class="nav navbar-nav">
						<li class="active"><a href="#" class="custom-menu-item">Home <span class="sr-only">(current)</span></a></li>
            <li class="dropdown">
            <a class="dropdown-toggle custom-menu-item" data-toggle="dropdown" id="about-us-menu" href="#" role="button" aria-haspopup="true" aria-expanded="false">About  Us <span class="caret"></span></a>
               <ul class="dropdown-menu" role="menu" aria-labelledby="about-us-menu">
                <li><a href="#">Action</a></li>
                <li><a href="#">Another action</a></li>
                <li><a href="#">Something else here</a></li>
                <li role="separator" class="divider"></li>
                <li><a href="#">Separated link</a></li>
               </ul>
            </li>
    
            <li class="dropdown ">
            <a class="dropdown-toggle custom-menu-item" data-toggle="dropdown" id="admin-info-menu" href="#" role="button" aria-haspopup="true" aria-expanded="false">Administration  <span class="caret"></span></a>
               <ul class="dropdown-menu" role="menu" aria-labelledby="admin-info-menu">
               <li><a href="#">Action</a></li>
                <li><a href="#">Another action</a></li>
                <li><a href="#">Something else here</a></li>
                <li role="separator" class="divider"></li>
                <li><a href="#">Separated link</a></li>
               </ul>
            </li>
					</ul>
				</div>
			</div>
		</div>
	</div>
</div>

答案 1 :(得分:3)

使用php Date and Time

这样的事情:

$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;

答案 2 :(得分:0)

只需删除* 365,您的代码就可以运行