Adding time in PHP

前端 未结 8 975
别那么骄傲
别那么骄傲 2020-12-22 10:37

I am pulling a datetime from a mysql db and i would like to add X hours to it then compare it to the current time. So far i got

$dateNow = strtotime(date(\'Y         


        
相关标签:
8条回答
  • 2020-12-22 11:10

    Assuming that the timestamp returned by the DB is in SQL format, the following should work fine:

    $dbTime = strtotime($row[0]);
    $nowTime = time();
    
    $future_dbTime = strtotime("+4 hours", $dbTime);
    
    $diff_time_seconds = $nowTime - $dbTime;
    
    if ($diff_time_seconds > 0) {
           echo "The current time is greater than the database time by:\n";
           $not_equal = true;
    
        }
    if ($diff_time_seconds == 0) {
           echo "The current time is equal to the database time!";
        }
    if ($diff_time_seconds < 0) {
           echo "The current time is less than the database time by:\n";
           $not_equal = true;
        }
    
    if ($not_equal) {
    $diff_time_abs_seconds = abs($diff_time_seconds);
    echo date('h:m:s', $diff_time_abs_seconds);
    }
    
    0 讨论(0)
  • 2020-12-22 11:11

    time() and strtotime() result in unix timestamps in seconds, so you can do something like the following, provided your db and do your comparison:

    $fourHours = 60 * 60 * 4;
    $futureTime = time() + $fourHours;
    
    0 讨论(0)
提交回复
热议问题