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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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