Jump to content

[SOLVED] Time value difference


sanchez77

Recommended Posts

I am trying to store two time values that are generated by the strftime().

 

I have no problem updateing the table\record with the second time value, but I can't figure out how to get the time difference between the two values.

 

so my code is like this, for updating the record, below is the timeout, i have one for timein

 

$format = '%d/%m/%Y %H:%M:%S';
$timeout = strftime($format);

mysql_query("UPDATE box SET timeout = '$timeout', WHERE boxno= '$_POST[boxno]'");

 

now after I update the record with timeout, I want to get the difference between timein and timeout.

 

Where do I begin?

 

Thanks for your help.

 

Link to comment
Share on other sites

You need to explain what 'timein' is, or where it will occur. Obviously to find the difference, you would do this though:

 

<?php
$difference= $timeout - $timein;
?>

 

Or vice versa, depending on which one occurs first. You didn't explain. ;)

Link to comment
Share on other sites

$timein1 = mysql_query("SELECT timein FROM boxes WHERE boxno = '$_POST[boxno]'");
$timeout1 = mysql_query("SELECT timeout FROM boxes WHERE boxno= '$_POST[boxno]'");
$difference = $timein1 - $timeout1;

echo "$timein1";
echo "$timeout1";
echo "$difference"; 

 

and it returns Resource id #4Resource id #5-1

Link to comment
Share on other sites

I am doing this for checkin and checkout

 


$format = '%d/%m/%Y %H:%M:%S';
$timein = strftime($format);
$status = 'taken';

mysql_query("UPDATE lockers SET timein = '$timein', timeout = '', status ='$status' WHERE lockerno = '$_POST[lockerno]'");

 

but my timein and timeout fields are varchar

 

can i still use TIMEDIFF

Link to comment
Share on other sites

   <?php

$format = '%d/%m/%Y %H:%M:%S';
$timeout = strftime($format);
$status = 'available';


mysql_query("UPDATE lockers SET timeout = '$timeout', status ='$status' WHERE lockerno = '$_POST[lockerno]'");

$difference = mysql_query("SELECT TIMEDIFF(`timeout`, `timein`) AS difference FROM `lockers` WHERE `lockerno`='{$_POST['lockerno']}'";

print $difference;

include "index.php";mysql_close($con)


?>   

Link to comment
Share on other sites

You need to read a mysql tutorial...

 

<?php
$difference = mysql_query("SELECT TIMEDIFF(`timeout`, `timein`) AS difference FROM `lockers` WHERE `lockerno`='{$_POST['lockerno']}'";//query is execueted

if(mysql_num_rows($difference) > 0)
{
//gor a row
$result = mysql_fetch_array($difference);//assign the results to an array
echo $result['difference'];
}

Link to comment
Share on other sites

i've been trying, but the code below returns a blank page. How can I make it return the error

 

<?php
include "connection.php";

$difference = mysql_query("SELECT TIMEDIFF('timein', 'timeout') AS difference FROM lockers WHERE lockerno='01'";//query is execueted

if(mysql_num_rows($difference) > 0)
{
//gor a row
$result = mysql_fetch_array($difference);//assign the results to an array
echo $result['difference'];
}

mysql_close($con);
?> 

 

 

Can this query still do the TIMEDIFF even thou the fields data type are VARCHAR?

Link to comment
Share on other sites

It should work fine, as long as they are the correct format (the same as datetime).

 

Let's try this,

 

change

 

if(mysql_num_rows($difference) > 0)
{
//gor a row
$result = mysql_fetch_array($difference);//assign the results to an array
echo $result['difference'];
}

 

to

 

<?php
if(mysql_num_rows($difference) > 0)
{
//got a row
$result = mysql_fetch_array($difference) or trigger_error("Error: ".mysql_error(), E_USER_ERROR);
;//assign the results to an array
echo $result['difference'];
}
else
{
echo 'no results';
}

Link to comment
Share on other sites

Thanks for your help, but when i use this, it returns no results. I checked the table and timein and timeout have values and there is a lockerno = 01

 

:facewall:

 


<?php
include "connection.php";

$difference = mysql_query("SELECT TIMEDIFF(`timein`, `timeout`) AS difference FROM lockers `lockerno` = 01 LIMIT 0, 30");//query is execueted

if(mysql_num_rows($difference) > 0)
{
//got a row
$result = mysql_fetch_array($difference) or trigger_error("Error: ".mysql_error(), E_USER_ERROR);
;//assign the results to an array
echo $result['difference'];
}
else
{
echo 'no results';
}

mysql_close($con);
?> 

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.