문제

sample:
    date1: 2019-04-13 03:00:00
    date2: 2019-09-02 19:00:00

date1에서 date2까지의 차이는?

해결

<?php
$result = mysql_query("SELECT (date1, date2) FROM myTable;");
while (list($date1, $date2) = mysql_fetch_array($result)) {
    $firstdate = new DateTime($date1);
    $seconddate = new DateTime($date2);
    $diff = $firstdate->diff($seconddate);
    echo $diff->d . " days, " . $diff->h . " hours, " . $diff->i . "minutes\n";
}
?>

dateTimeDiff 함수화

<?php
public static function dateTimeDiff($inputDate, $type = Null) {
        $now = new DateTime();
        $inputDate = new DateTime($inputDate);

        switch ($type) {
            case 'Hours' :
                $result = $now->diff($inputDate)->days * 24 + $now->diff($inputDate)->h;
                break;
            case 'Days' :
                $result = $now->diff($inputDate)->days;
                break;
            case 'Months' :
                $result = $now->diff($inputDate)->y * 12 + $now->diff($inputDate)->m;
                break;
            case 'Years' :
                $result = $now->diff($inputDate)->y;
                break;
            case 'Raw' :
                $result = $now->diff($inputDate);
                break;
            default :
                $result = $now->diff($inputDate)->days * 86400 + $now->diff($inputDate)->h * 3600 + $now->diff($inputDate)->i * 60 + $now->diff($inputDate)->s;
                break;
        }

        return $result;
    }
@if(Common::dateTimeDiff($row->date, 'Days') <= 0 && !empty($list))
/* 코드 실행 */
@endif

Common::dateTimeDiff($row->date, 'Days') == 0 : 오늘