Hobbyist_PHPer Posted May 17, 2012 Share Posted May 17, 2012 Hi, so I am trying to get how many minutes and seconds are left from the datetime entry in the database and the current datetime, but I'm having issues... Here's my code: $query = "SELECT RequestMadeDateTime FROM TempAwaitingClients WHERE PSOID = '2'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $RequestMadeDateTime = $row['RequestMadeDateTime']; $currentDateTime = date('Y-m-d H:i:s'); echo 'Request Made Date Time: ' . $RequestMadeDateTime . '<br />'; echo 'Current Date Time: ' . $currentDateTime . '<br />'; echo date_diff($RequestMadeDateTime, $currentDateTime); What I'm getting those is this, "Warning: date_diff() expects parameter 1 to be DateTime" ... Which I don't get because both variables are DateTime... Quote Link to comment https://forums.phpfreaks.com/topic/262672-datetime-date_diff-function/ Share on other sites More sharing options...
trq Posted May 17, 2012 Share Posted May 17, 2012 It expects a DateTime object. it has nothing to do with the fact that your mysql datatype is datetime or not. Quote Link to comment https://forums.phpfreaks.com/topic/262672-datetime-date_diff-function/#findComment-1346322 Share on other sites More sharing options...
Hobbyist_PHPer Posted May 17, 2012 Author Share Posted May 17, 2012 It expects a DateTime object. it has nothing to do with the fact that your mysql datatype is datetime or not. Oh, I feel stupid... the PHP docs weren't very intuitive on this function... Don't suppose you have a quick and dirty solution at the top of your head, do ya, for getting the difference in minutes and seconds? If not, that's cool... I'll keep searching and playing.... Quote Link to comment https://forums.phpfreaks.com/topic/262672-datetime-date_diff-function/#findComment-1346323 Share on other sites More sharing options...
Hobbyist_PHPer Posted May 17, 2012 Author Share Posted May 17, 2012 I figured it out... Yeah! $RequestMadeDateTime = date('Y-m-d H:i:s', strtotime($row['RequestMadeDateTime'])); $currentDateTime = date('Y-m-d H:i:s'); echo 'Request Made Date Time: ' . $RequestMadeDateTime . '<br /> Current Date Time: ' . $currentDateTime . '<br /><br /><br />'; $theRequestMadeDateTime = strtotime($RequestMadeDateTime); $theCurrentDateTime = strtotime($currentDateTime); echo 'Request Made Date Time: ' . $theRequestMadeDateTime . '<br /> Current Date Time: ' . $theCurrentDateTime . '<br /><br /><br />'; $theDifferenceInSeconds = 600 - ($theCurrentDateTime - $theRequestMadeDateTime); if ($theDifferenceInSeconds > 60) { $minutesLeft = (floor ($theDifferenceInSeconds / 60)); $secondsLeft = $theDifferenceInSeconds - ($minutesLeft * 60); } else { $minutesLeft = 0; $secondsLeft = $theDifferenceInSeconds; } echo 'Minutes Left: ' . $minutesLeft . '<br /> Seconds Left: ' . $secondsLeft . '<br /><br /><br />'; Quote Link to comment https://forums.phpfreaks.com/topic/262672-datetime-date_diff-function/#findComment-1346364 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.