Terminaxx Posted August 1, 2017 Share Posted August 1, 2017 (edited) Hey Guys. I got a timestamp saved in my database. Now i want to show the Timediffernce between now and the timestamp saved. I tried this: $date1 = time(); $date2 = $enddate; $diffDate = $date2 - $date1; $days = floor($diffDate / 24 / 60 / 60 ); $diffDate = $diffDate - ($days*24*60*60); $hours = floor($diffDate / 60 / 60); $diffDate = ($diffDate - ($hours*60*60)); $minutes = floor($diffDate/60); $diffDate = $diffDate - ($minutes*60); $seconds = floor($diffDate); echo "Time: ".$days."d ".$hours."h ".$minutes."m ".$seconds."s"; But it doesnt work. It shows something like this -19383d 16h 33m 37sThanks for any help Edited August 1, 2017 by Terminaxx Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 1, 2017 Share Posted August 1, 2017 Are you using MySQL? If so, you could use the built-in datediff() and now() functions: https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html Quote Link to comment Share on other sites More sharing options...
Terminaxx Posted August 1, 2017 Author Share Posted August 1, 2017 (edited) Could you show me an example on how to do it and also show the results in days, hours, minutes and seconds?I would be very grateful Edited August 1, 2017 by Terminaxx Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 1, 2017 Share Posted August 1, 2017 There are a number of examples online. https://www.google.com/search?q=mysql+datediff+current+date Quote Link to comment Share on other sites More sharing options...
Terminaxx Posted August 1, 2017 Author Share Posted August 1, 2017 But the only thing i can do with it is see the DATEdifference between it. But i would also like to see the hour, minute and second diffrences. Is this somehow possible? Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted August 1, 2017 Solution Share Posted August 1, 2017 (edited) There's no such feature in MySQL. The DATEDIFF() function gives you calendar days and has nothing to do with time. Use the DateTime class in your application. <?php const MYSQL_DATETIME_FORMAT = 'Y-m-d G:i:s'; $input = '2017-07-31 19:15:74'; $now = new DateTime(); $input_datetime = DateTime::createFromFormat(MYSQL_DATETIME_FORMAT, $input); $diff = $now->diff($input_datetime); echo $diff->format('%d days, %H hours, %I minutes, %S seconds'); Edited August 1, 2017 by Jacques1 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted August 1, 2017 Share Posted August 1, 2017 Perhaps this is a better search then: https://www.google.com/search?q=mysql+datediff+current+date+hours+minutes+seconds I'm seeing examples that utilize the time_format() function in MySQL. https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_time-format Quote Link to comment Share on other sites More sharing options...
Terminaxx Posted August 1, 2017 Author Share Posted August 1, 2017 There's no such feature in MySQL. The DATEDIFF() function gives you calendar days and has nothing to do with time. Use the DateTime class in your application. <?php const MYSQL_DATETIME_FORMAT = 'Y-m-d G:i:s'; $input = '2017-07-31 19:15:74'; $now = new DateTime(); $input_datetime = DateTime::createFromFormat(MYSQL_DATETIME_FORMAT, $input); $diff = $now->diff($input_datetime); echo $diff->format('%d days, %H hours, %I minutes, %S seconds'); Thank you once again for a good answer with an example given. 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.