Jump to content

date diff


sanchez77

Recommended Posts

I have two dates fields stored as VARCHAR in table.

 

How can I query that and return the difference in days?

 

I was using this below, but it's not returning a value. Is there a differernt SELECT statement i can use?

 


$difference = mysql_query("SELECT TIMEDIFF(`storedatein`, `storetimeout`) AS difference FROM lockers WHERE `lockerno` = '$_POST[lockerno]'");//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 "The locker number " . $_POST[lockerno] . " was rented for " . $result['difference'];
}
else
{
echo 'no results';
}


Link to comment
https://forums.phpfreaks.com/topic/168713-date-diff/
Share on other sites

By storing them in varchar format you can not compare them since your script just looks at them as strings rather than actual dates.

 

If you change the column type in mysql to date you will then be able to compare the values.

 

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html

Link to comment
https://forums.phpfreaks.com/topic/168713-date-diff/#findComment-890192
Share on other sites

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.