How to make a check if this timestamp is today,tom

2020-06-12 05:28发布

I would like to know how to check if the timestamp is today, tomorrow or the day after tomorrow.

I have e.g. :

$timestamp = "1313000474";

How to make a check if this timestamp is today,tomorrow or the day after tomorrow?

e.g.

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

How to do this?

EDIT: corrected unix timestamp

Thank you in advance.

标签: php date
4条回答
做个烂人
2楼-- · 2020-06-12 05:46
$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";
}
查看更多
狗以群分
3楼-- · 2020-06-12 05:46
<?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";
}
查看更多
\"骚年 ilove
4楼-- · 2020-06-12 05:52
<?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";
    }
  }
?>
查看更多
▲ chillily
5楼-- · 2020-06-12 06:07

Keep your code clean...

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

EDIT: With this you dont have to worry if the timestamp is already without hours, minutes and seconds... Or create different output dates, replacing echo $compare_date_desc; by echo date($compare_date_desc,$timestamp);

查看更多
登录 后发表回答