Jump to content

Subtract 2 datetimes


golffor1

Recommended Posts

Hello

I have 2 datetimes...both are inside a database. The datetimes are not unix time they are regular date\times.

 

So for instance...in the database I have a field called Login_time which is 2012-01-30 02:43:13

 

I am taking the current time in the same format with the following code

$curDate = date("Y-m-d H:i:s", time()); 

The problem is that I can't figure out how to tell the exact minutes for the 2 date\time intervals.

 


//find out if the ip address is inside the database
$doesIpExsist = mysql_query("Select * from login_information where IP_Address like '$ip'", $link); 
$num_rows = mysql_num_rows($doesIpExsist);

echo "$num_rows Rows\n";

if ($num_rows == 0)
{
   //create the field inside the database
   $ip_query = "INSERT INTO login_information (IP_address, `Login_attempts`, Login_time, Total_login_attempts) VALUES ('$ip', '1', NOW(), '1')";
   $ip_result = mysql_query($ip_query, $link) or die (mysql_error());
}
else  //the fields alaready exsist
{
$curDate = date("Y-m-d H:i:s", time());

echo $curDate;


  include ('timefunction.php');
   echo "<br>";
  echo "IP ".$ip;
  echo "<br>";
     $select_ip_id = "SELECT ID from login_information where IP_address like '127.0.0.1'";
   //$select_ip_id = "SELECT ID from login_information where IP_address like '$ip'";
   echo $select_ip_id;
   $ip_error = mysql_query($select_ip_id, $link) or die (mysql_error());
   foreach ($row = mysql_fetch_array($ip_error, MYSQL_ASSOC) as $key => $value)
   {
   echo dateDiff('$curDate', '$get_ip_error') . "<br>";
echo dateDiff('$get_ip_error', '$curDate') . "<br>";
         echo "HIoROW " . $value. "<br>";  
   }
   $get_ip_time = "Select Login_time from login_information where IP_address like '$ip'";
   $get_ip_error = mysql_query($get_ip_time, $link) or die (mysql_error());
   
   //echo $get_ip_error;
   /*while ($row = mysql_fetch_array($get_ip_error, MYSQL_ASSOC))
//while($row=mysql_fetch_array($get_ip_error))
{
    echo $row;
   }
*/

echo "HI";
echo $curDate;
echo "<br>";
echo $get_ip_error;
echo "<br>";
echo dateDiff('$curDate', '$get_ip_error') . "<br>";
echo dateDiff('$get_ip_error', '$curDate') . "<br>";
   

echo "<br>";

//$datetime1 = date_create($curDate);
//$datetime2 = date_create($get_ip_error);
//$interval = date_diff($datetime1, $datetime2);
//echo $interval->format('%R%a days');


//$dt1 = new DateTime($get_ip_error);
//$dt2 = new DateTime($curDate);

//$dtdiff = $dt1->diff($dt2);
//print $dtdiff->format('%h hours, %i minutes');  
//echo "<br>";



   
   //$timeDifference = time_diff ($get_p_error, $curDate);
   //echo $timeDifference;
   //$timeDifference = $get_ip_error - $curDate;
   //echo "<br>";
   //echo $timeDifference;
   //mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'");
   
   //$ip_query = "UPDATE INTO login_information (IP_address, `Login_attempts`, Login_time, Total_login_attempts) VALUES ('$ip', '1', NOW(), '1')";
echo "So far so good";


}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>

</body>
</html>

 

Currently it doesn't work to the minutes.  Can you tell me what i am doing wrong on this issue.

Link to comment
Share on other sites

When you use the DateTime class and its diff method:

 

<?php
$date1 = new DateTime('2012-01-30 02:43:13');
$date2 = new DateTime('2012-01-31 05:00:00');
$interval = $date1->diff($date2);
echo $interval->format("%I minutes");  # prints 16 minutes (which is only part of the difference)
?>

 

This is working out the time/date difference in terms of years, months, days etc. so if you're only using minutes (like above), and the time difference is more than 60 minutes you're ignoring a large portion of the time.  E.g. if the time difference were 70 minutes, the above would show only 10 minutes since it stashes the rest of the time as 1 hour - e.g. 1 hour and 10 minutes.

 

If you want the exact number of minutes between two dates from the database, you could do something like:

 

<?php
$time1 = strtotime('2012-01-30 02:43:13');  # gets a timestamp (in seconds)
$time2 = strtotime('2012-01-31 05:00:00');

$diff = $time2 - $time1;

printf("The difference between time1 and time2 is %d minutes", ($diff/60));  # 60 seconds in a minute
exit;
?>

 

I hope that helps solve your problem - I got a little lost reading the code you provided.

Link to comment
Share on other sites

Hello,

Sorry I wasn't clear in the problem that I am running into. 

I have done your suggestion and it works fine if i put the dates & times in there myself.  If i grab 1 date & time from a database and set it to variable, and then set the current date & time to a variable.  It always gives me 0 minutes, this seems to be where my issue is that I am running into.  I am not sure how to display the difference in the times that way.

Link to comment
Share on other sites

It sounds like your variables are not what you think they are or should be.  Try echo the variable containing the date from the database and also your constructed date to make sure they are in the format you expect and contain the date/time you expect.

 

As long as these values are in the correct format, the code I provided should work the difference in minutes out just fine.  Just keep in mind that if the difference between the times is less than 1 minute, it will say 0 minutes, not something like 0.5 minutes.

 

If you still have problems, post your updated code here and I'll take a closer look.

Link to comment
Share on other sites

SELECT TIMEDIFF(NOW(), login_time) AS time_since_last FROM login_inf . . .

 

I was not aware that a Timediff in the select statement would work.  I have been able to remove a lot of lines of code with this.

 

My code is now working properly.

 

 

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.