Jump to content

DateTime => date_diff function


Hobbyist_PHPer

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/262672-datetime-date_diff-function/
Share on other sites

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....

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 />';

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.