SystemOverload Posted August 25, 2009 Share Posted August 25, 2009 I have a MySQL field 'orgDateAmended' I need to obtain the number of days since the record was amended in PHP. In plain english [currentdate] - [orgDateAmended] = x << i want 'x' I have no issue retrieving the date in MySQL, I just need to know how to subtract the two once I have the orgDateAmended assigned to a PHP Variable. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/171852-subtract-two-dates-and-get-an-integer/ Share on other sites More sharing options...
ignace Posted August 25, 2009 Share Posted August 25, 2009 SELECT now() - orgDateAmended AS x FROM table Quote Link to comment https://forums.phpfreaks.com/topic/171852-subtract-two-dates-and-get-an-integer/#findComment-906145 Share on other sites More sharing options...
SystemOverload Posted August 25, 2009 Author Share Posted August 25, 2009 Is it not easier to do it in PHP with PHP functions? Quote Link to comment https://forums.phpfreaks.com/topic/171852-subtract-two-dates-and-get-an-integer/#findComment-906150 Share on other sites More sharing options...
ignace Posted August 25, 2009 Share Posted August 25, 2009 Is it not easier to do it in PHP with PHP functions? That actually doesn't matter but given you are retrieving the data from MySQL you might aswell do the conversion in your query so that you can start working with it in PHP Quote Link to comment https://forums.phpfreaks.com/topic/171852-subtract-two-dates-and-get-an-integer/#findComment-906185 Share on other sites More sharing options...
SystemOverload Posted August 25, 2009 Author Share Posted August 25, 2009 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/171852-subtract-two-dates-and-get-an-integer/#findComment-906214 Share on other sites More sharing options...
SystemOverload Posted September 2, 2009 Author Share Posted September 2, 2009 Ok, I get an integer (74891990) from now()-date_amended, which I presumed was seconds, but with a date of 2009-08-27 23:22:18, now()-this date should result in approx 483,304.79 seconds (8055 mins or 134 hrs or 5.59 days). If 74891990 were seconds, it would result in 866 days, ie: way out. Can someone explain why I'm getting this figure and give me a hint where I'm going wrong please. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/171852-subtract-two-dates-and-get-an-integer/#findComment-910887 Share on other sites More sharing options...
Mark Baker Posted September 2, 2009 Share Posted September 2, 2009 What data type are you using for the orgDateAmended column in your table? Quote Link to comment https://forums.phpfreaks.com/topic/171852-subtract-two-dates-and-get-an-integer/#findComment-910927 Share on other sites More sharing options...
SystemOverload Posted September 2, 2009 Author Share Posted September 2, 2009 A - DATETIME B - Someone on another forum has provided the following solution: UNIX_TIMESTAMP() - UNIX_TIMESTAMP(orgDateAmended) AS x which appears to work just fine, unless you can see any issues with doing it this way? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/171852-subtract-two-dates-and-get-an-integer/#findComment-910931 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.