简体   繁体   中英

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

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

I have eg :

$timestamp = "1313000474";

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

eg

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.

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

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

<?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";
}
<?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";
    }
  }
?>

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