stangn99 Posted December 16, 2010 Share Posted December 16, 2010 Hey guys, How would I go about subtracting Today from a previous day to find the difference? For example, I want to subtract TODAY from a previous date in my database, to determine if the difference is greater than 1 day. Any ideas? I tried doing the subraction in TIMESTAMPS, but when I convert the date back to Y-m-d H:i:s, I got some weird year and time. Link to comment https://forums.phpfreaks.com/topic/221891-subtract-date-and-time-from-another-date-and-time-display-difference/ Share on other sites More sharing options...
johnny86 Posted December 16, 2010 Share Posted December 16, 2010 <?php // Create DateTime object with your timestamp $date = DateTime::createFromFormat("Y-m-d H:i:s", '2010-02-15 15:16:17'); // Calculate the difference between your timestamp and "now" $interval = $date->diff(new DateTime()); // Get the data stored in the DateInterval echo $interval->format("Differernce is %d days, %h hours %i minutes and %s seconds."); ?> Link to comment https://forums.phpfreaks.com/topic/221891-subtract-date-and-time-from-another-date-and-time-display-difference/#findComment-1148212 Share on other sites More sharing options...
PFMaBiSmAd Posted December 16, 2010 Share Posted December 16, 2010 Supprizingly, there is a function to do this directly in your query - http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff No slow php code is needed. Link to comment https://forums.phpfreaks.com/topic/221891-subtract-date-and-time-from-another-date-and-time-display-difference/#findComment-1148218 Share on other sites More sharing options...
stangn99 Posted December 16, 2010 Author Share Posted December 16, 2010 Thanks for the information guys. I looked at the MYSQL page on DateDiff. I'm going to try this route first. I'll post back if I hit any speed bumps. Thanks again. Link to comment https://forums.phpfreaks.com/topic/221891-subtract-date-and-time-from-another-date-and-time-display-difference/#findComment-1148243 Share on other sites More sharing options...
stangn99 Posted December 16, 2010 Author Share Posted December 16, 2010 Ok..so i'm stuck. How would I use mysql's DATEDIFF to subtract a date in the database called 'expire' by the current date and time? This doesn't work: $result = mysql_query ("SELECT * FROM sample WHERE username = '$username' AND DATEDIFF('$today', 'expire'"); Link to comment https://forums.phpfreaks.com/topic/221891-subtract-date-and-time-from-another-date-and-time-display-difference/#findComment-1148260 Share on other sites More sharing options...
johnny86 Posted December 16, 2010 Share Posted December 16, 2010 Try: $result = mysql_query("SELECT * FROM sample WHERE username = '$username' AND DATEDIFF(NOW(), expire) > 1"); Link to comment https://forums.phpfreaks.com/topic/221891-subtract-date-and-time-from-another-date-and-time-display-difference/#findComment-1148269 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.