0

How to make a check if datetime from my db is today and then echo with "today" and if my datetime is tomorrow echo with tomorrow. I want to make this for a date reminder for fullcalendar.

My Php code is not working or is messed:

$reminder = date('d-M-Y',strtotime(date("d-m-Y", strtotime($row['start'])) . " +0 days"));
$tomorrow = strtotime("+1 day");
$tomorrow = getdate($tomorrow);

if($reminder = "0 Days") {
    echo "Today";
} else if($reminder = "1 Days" ) {
    echo "Tomorrow";
} else {
    echo (strtotime($reminder) - strtotime(date('d-M-Y'))) / (60 * 60 * 24).' Days';
}
julianstark999
  • 3,450
  • 1
  • 27
  • 41
Alex
  • 47
  • 8

4 Answers4

0

You can try it like this:

$diff = date_diff(date('d-M-Y'), date('d-M-Y', strtotime($row['start'])));

if($diff == 0) {
    echo "Today";
} else if($diff == 1) {
    echo "Tomorrow";
}
julianstark999
  • 3,450
  • 1
  • 27
  • 41
Naveed Ramzan
  • 3,565
  • 3
  • 25
  • 30
0

Check your if-statements, you are setting rather than checking.

Replace your = with == in your ifs:

if($reminder == "0 Days") {
    echo "Today";
} else if($reminder == "1 Days" ) {
    echo "Tomorrow";
} else {
    echo (strtotime($reminder) - strtotime(date('d-M-Y'))) / (60 * 60 * 24).' Days';
}
brombeer
  • 8,716
  • 5
  • 21
  • 27
0
<?php

$date='14-Nov-2017';
$time = strtotime($date);
$formatDate = date('Y-m-d',$time);
echo $formatDate;
$today=date("Y-m-d");

if($formatDate==date("Y-m-d")){
    echo "TODAY";
}
else if ($formatDate==date('Y-m-d', strtotime($today. ' +1 days'))){
    echo 'TOMORROW';
}
else{
    echo "Another day";
}

This code get's the date you output , and compares it with todays date , tomorrows date otherwise just echos another date. Tested and it works also.

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
0

we can get Date Difference using diff method of DateTime class.

I wrote common function for that. just you need to pass Date/DateTime String format or instance of DateTime class

<?php
    $currentDate = '2022-07-27';
    // $nextDate = '2022-07-27';
    // $nextDate = '2022-07-28';
    $nextDate = '2022-07-29';
    echo getDateToTodayTomorrowFormat($nextDate, $currentDate);


    /**
     * get Date format to number of Days difference Format ('Today', 'Tomorrow', '2 days later', '3 days later' etc.)
     *
     * @param  string|\DateTime $nextDate // eg. pass Object of DateTime class "OR" Date/Date-Time string (format :- 'Y-m-d', 'Y/m/d', 'Y-m-d H:i:s') 
     * @param  string|\DateTime $currentDate // eg. pass Object of DateTime class "OR"  Date/Date-Time string (format :- 'Y-m-d', 'Y/m/d', 'Y-m-d H:i:s') 
     * @return string
     * 
     * Date/Date-Time string must be in format of standard formats which are supported by \DateTime::class (https://www.php.net/manual/en/class.datetime.php)
     * 
     * ****
     * # Input > Output :- 
     *  $currentDate = '2022-07-27';
     * 
     *      case 1:- $nextDate = '2022-07-27',  `then OUTPUT :- "Today"` \
     *      case 2:- $nextDate = '2022-07-28',  `then OUTPUT :- "Tomorrow"` \
     *      case 3:- $nextDate = '2022-07-29',  `then OUTPUT :- "2 days later"`
     */
    function getDateToTodayTomorrowFormat($nextDate, $currentDate)
    {
        if (is_string($nextDate)) {
            $nextDate = new \DateTime($nextDate);
        }
        
        if (is_string($currentDate)) {
            $currentDate = new \DateTime($currentDate);
        }
        
        $interval = $currentDate->diff($nextDate);

       // $days = $interval->format('%r %d'); // returns like "- 1", "0", "+ 1"

        $days = $interval->days; // returns only 0 & int difference
        if($days === 0) {
            return 'Today';
        } else if($days === 1) {
            return 'Tomorrow';
        } else {
            return $days . ' days later';
        }
    }
?>

for more details see this stackoverflow question

Harsh Patel
  • 1,032
  • 6
  • 21