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
https://forums.phpfreaks.com/topic/168451-solved-time-value-difference/
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

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

   <?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)


?>   

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'];
}

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?

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

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);
?> 

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.