Paul-D Posted August 27, 2018 Share Posted August 27, 2018 Hi I have 2 MySQL date times I want to subtract them and display the number of hours minutes and seconds (seconds optional) I could have days but would be happy if the hours were more than 24. Thanks. Paul. Quote Link to comment Share on other sites More sharing options...
requinix Posted August 27, 2018 Share Posted August 27, 2018 There might be something useful here. Quote Link to comment Share on other sites More sharing options...
Paul-D Posted August 27, 2018 Author Share Posted August 27, 2018 Thanks for these. These are useful functions but in my case I am extracting to date time values from different tables not in the same SQL so I think I need a PHP solution. $a = "2018-08-26 12:20:00" $b = "2018-08-27 13:30:05" So $c would be either 1 day : 1 hour: 10 minutes: and 5 seconds OR 25 hours 10 minutes: 5 seconds. Sorry not so strait forward. Quote Link to comment Share on other sites More sharing options...
Paul-D Posted August 27, 2018 Author Share Posted August 27, 2018 Maybe turn them into a time stamp subtract them and just print out $c = date('d H:i:s' ,$time); Quote Link to comment Share on other sites More sharing options...
requinix Posted August 27, 2018 Share Posted August 27, 2018 Oh, you want to do this in PHP. One big question here: what about DST? In the US it will end Nov 4. If you had two dates, Nov 3 12pm and Nov 4 12pm, would you want the result to be 24 hours (because that's what it looks like) or 25 hours (because that's the actual difference)? Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 27, 2018 Share Posted August 27, 2018 29 minutes ago, Paul-D said: These are useful functions but in my case I am extracting to date time values from different tables not in the same SQL so I think I need a PHP solution. That shouldn't matter. You could run a query to get the first date/time and assign the value to a PHP variable. In your second query you could execute the TIMEDIFF() function where one argument is the PHP variable created earlier. The other argument would be the other date/time. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 27, 2018 Share Posted August 27, 2018 Or consider using a join, so you have a single query on the two tables instead of two separate queries. Quote Link to comment Share on other sites More sharing options...
maxxd Posted August 28, 2018 Share Posted August 28, 2018 If you're specifically looking for a PHP solution, DateTime::diff() sounds about perfect. You'll have your difference, but be able to format that difference in a variety of ways. Unless I'm not understanding the issue. Quote Link to comment 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.