Adding time in PHP

前端 未结 8 1077
别那么骄傲
别那么骄傲 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:02

    Probably the safest way to do the compare is right in the SQL

    SELECT * FROM my_table WHERE someDateTimeColumn < DATE_ADD(NOW(), INTERVAL 4 hour)
    

    And since you're assembling it in PHP, you can dynamically replace the "4 hour" bit with whatever your code needs to compare.

    (Note: putting the entire calculation on the other side of the comparison to the column allows MySQL to do the calculation once per query, rather than once per row, and also use the table's index, if that column has one.)

提交回复
热议问题