natasha_sharma Posted March 15, 2016 Share Posted March 15, 2016 (edited) Hi, I want to update a column in my table with the difference between two dates. resultdate variable is in yyyy-mm-dd update table test_table set days_diff = (resultdate - CURDATE()) where 1 = 1; So, i want to reset my days_diff variable with the latest date when the code is run. But this code is given me correct days difference in first run, but when i instantly run this code, I get wrong number of differences. Is there any better code to to do this ? Edited March 15, 2016 by natasha_sharma Quote Link to comment https://forums.phpfreaks.com/topic/301007-diffrence-in-days-in-two-dates/ Share on other sites More sharing options...
Solution Barand Posted March 15, 2016 Solution Share Posted March 15, 2016 You need to use the DATEDIFF() function http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_datediff but the best way to do it is not to do it at all. Derived data should not be stored in your database. Calculate it when required instead of continually updating the table. Quote Link to comment https://forums.phpfreaks.com/topic/301007-diffrence-in-days-in-two-dates/#findComment-1532027 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.