Jump to content

Store, get and calculate date in PHP & MySql


Recommended Posts

Hello, I'm working on a book-library and I have to store dates in the DB, get the date from the DB add 30 days and then store it again. Why am I having such a problem doing this. Man, I've tryed loads of solutions, but nothing works. I'd be most greatful if anyone could help me?

The conditions are: you have to return book before you can get another loan.

Here's the code:

include "conn.php";

$query = 'SELECT memberid FROM member '
. 'WHERE username = \''. $_SESSION['authUser'] .'\'';

$result = mysql_query($query) or die(mysql_error());

$values = mysql_fetch_assoc($result);

$_SESSION['user'] = $values;

$membid = $_SESSION['user']['memberid'];
$bid = $_POST['bokid'];
$date = date("Y:m:d");
$date2 = date("Y:m:d",strtotime('+30 days'));;

// Check if member already has a book
$check = mysql_query("SELECT COUNT(*) AS hit FROM loan WHERE memberid='$membid'");

// Check if book is lent by someone else
$check2 = mysql_query("SELECT COUNT(*) AS hit FROM loan WHERE bookid='$bid'");

// Get returning date if some one else has the book
$ater = mysql_query("SELECT _STR_TO_DATE(returndate,'%d/%m/%Y') as 'returndate' FROM loan WHERE bookid='$bid'");

if (mysql_result($check, 0, 'hit') == 1) {
// If the logged in member already has lent a book
echo 'Please return book first!';

elseif (mysql_result($check2, 0, 'hit') == 1) {
// If selected book is lent by other, new loan is registerd and the date of the loan is set to the same as //returning date (of the member who has it now)
mysql_query("INSERT INTO loan (loandate, memberid, bookid, returndate) VALUES
('$return', '$membid', '$bid', '$return + 30')");
echo 'You'll get the book as soon as it is returned from other member.';
else {
// Book is in library. Loan-date is set to todays date and returning date is set to today+30 days
mysql_query("INSERT INTO loan (loandate, memberid, bookid, returndate) VALUES
('$date', '$membid', '$bid', '$date2'");
echo 'Soon come!';

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

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

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.