简体   繁体   中英

Php check if my date from db is today echo with today and if tomorrow then echo tomorrow

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

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

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

Replace your = with == in your if s:

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';
}
<?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.

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

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