Jump to content

Archived

This topic is now archived and is closed to further replies.

djuhr

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:


<?
session_start();
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!';
}



?>

Share this post


Link to post
Share on other sites

×

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.