如何查看此时间戳是今天,明天还是后天?

时间:2011-08-10 17:16:36

标签: php date

我想知道如何检查时间戳是今天,明天还是后天。

我有例如:

$timestamp = "1313000474";

如何检查此时间戳是今天,明天还是后天?

e.g。

if today then echo $output = "today";
if tomorrow then echo $output = "tomorrow";
if the day after tomorrow then echo $output = "dayaftertomorrow";

怎么做?

编辑:更正了unix时间戳

提前谢谢。

4 个答案:

答案 0 :(得分:24)

$timestamp = "1313000474";

$date = date('Y-m-d', $timestamp);
$today = date('Y-m-d');
$tomorrow = date('Y-m-d', strtotime('tomorrow')); 
$day_after_tomorrow = date('Y-m-d', strtotime('tomorrow + 1 day'));

if ($date == $today) {
  echo "today";
} else if ($date == $tomorrow) {
  echo "tomorrow";
} else if ($date == $day_after_tomorrow) {
  echo "dayaftertomorrow";
}

答案 1 :(得分:1)

<?php
$time = "20060713174545";

$date = date('Y-m-d', strtotime($time));
$now = date('Y-m-d');
$tomorrow = date('Y-m-d', time() + strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', time() + strtotime('tomorrow + 1 day'));
if ($date == $now){
    echo "It's today";
} 
elseif($date == $tomorrow){
    echo "It's tomorrow";
}
elseif($date == $day_after_tomorrow){
    echo "It's day after tomorrow";
}
else{
    echo "None of previous if statements passed";
}

答案 2 :(得分:1)

保持代码清洁......

$timestamp = "1313000474";

// Description demonstrate proposes only...
$compare_dates = array(
    'today' => 'today!!',
    'tomorrow' => 'Tomorrow!!!',
    'tomorrow +1 day' => 'day after tomorrow? YEAH', 
);

foreach($compare_dates => $compare_date => $compare_date_desc){
    if(strtotime($compare_date) > $timestamp && $timestamp < strtotime($compare_date.' +1 day') ){
        echo $compare_date_desc;
        break;
    }
}

编辑:有了这个,您不必担心时间戳是否已经没有小时,分钟和秒......或者创建不同的输出日期,将echo $compare_date_desc;替换为echo date($compare_date_desc,$timestamp);

答案 3 :(得分:-1)

<?php
function getTheDay($date)
{
   $curr_date=strtotime(date("Y-m-d H:i:s"));
   $the_date=strtotime($date);
   $diff=floor(($curr_date-$the_date)/(60*60*24));

switch($diff)
  {
     case 0:
     return "Today";
         break;
     case 1:
     return "Yesterday";
        break;
     default:
        return $diff." Days ago";
    }
  }
?>