/    Sign up×
Community /Pin to ProfileBookmark

[FONT=Arial][COLOR=Blue]I try to develop a library information system. The only thing that becoming a problem form is when someone borrowing the books and return it. if he or she give the book back before the expired date he or she will not accept any fine from the library. for example he borrow the book on 10th July 2005 and return it one week later, mean that on 17th July, if he return it on 20th July 2005 he will get fine for 3 days. My answer is, how to know that he get fine for 3 days in PHP. I mean, how to process the date he borrow the book and the date he return it… [/COLOR][/FONT]

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@bokehAug 31.2005 — When the book is borrowed run the following:
[code=php]<?php
// When book borrowed store following value in MySQL
$book_borrowed = time();
?>[/code]

and when it is returned run this:
[code=php]<?php
//When book returned
$book_borrowed = ''; // Retrieve value from MySQL
$midnight_one_week_later = (($book_borrowed - ($book_borrowed % (60 * 60 * 24))) + ((60 * 60 * 24) - date('Z')) + (60 * 60 * 24 * 7));
if(time() > $midnight_one_week_later){
$days = ceil((time() - $midnight_one_week_later)/(60 * 60 * 24));
if(1 == $days){
print 'The book is 1 day late.';
}else{
print 'The book is '.$days.' days late.';
}
}else{
print 'The book is not late.';
}

?>[/code]
Copy linkTweet thisAlerts:
@ffurnaiAug 31.2005 — This brings up a good question. How do you subtract two date strings from each other and tell how many days difference there is?

for example:
[CODE]$d1 = '20050827';
$d2 = '20050831'; [/CODE]
Copy linkTweet thisAlerts:
@NogDogAug 31.2005 — Use strtotime($d1) to get unix timestamp, then do your math on the timestamps and convert back to months/days/etc. using date(), along the lines that Bokeh showed above.
Copy linkTweet thisAlerts:
@bokehAug 31.2005 — This brings up a good question. How do you subtract two date strings from each other and tell how many days difference there is?

for example:
[CODE]$d1 = '20050827';
$d2 = '20050831'; [/CODE]
[/QUOTE]


It's a good question but in the question above the original post was about library books so I decided that crossing midnight would count as one day. You may or may not agree with this but that is what I based the above on. If you run the following you will see what I mean:[code=php]<?php // $d1 & $d2 here are base on unix timestamp as returned by time();
$d1 = '1124523006';
print '$d1 = '.print_date($d1).'<br>';

$midnight_after_d1 = ($d1 - ($d1 % (60 * 60 * 24))) + ((60 * 60 * 24) - date('Z'));
print '$midnight_after_d1 = '.print_date($midnight_after_d1).'<br>';

$d2 = '1125523006';
print '$d2 = '.print_date($d2).'<br>';

$midnight_after_d2 = (($d2 - ($d2 % (60 * 60 * 24))) + ((60 * 60 * 24) - date('Z')));
print '$midnight_after_d2 = '.print_date($midnight_after_d2).'<br>';

print 'The number of midnights between those two values is '.ceil(($midnight_after_d2/86400) - ($midnight_after_d1/86400));


function print_date($timestamp)
{
return date('l jS F Y H:i:s', $timestamp);
}

?>[/code]
Copy linkTweet thisAlerts:
@ffurnaiSep 01.2005 — I tried this, using what I thought was the correct logic. But look at the result.

[code=php]<?php
$d1 = '20050827';
$d1date = strtotime($d1);
$d2 = '20050901';
$d2date = strtotime($d2);

$diff = $d1date - $d2date;


echo $diff;
?>[/code]


it results in this: -432000

That doesn't look right . . . should be 4. What is the 32000?
Copy linkTweet thisAlerts:
@bokehSep 01.2005 — [code=php]<?php
//By the way it's five days not four.
$d1 = '20050827';
$d1date = strtotime($d1);
$d2 = '20050901';
$d2date = strtotime($d2);

$diff = $d2date - $d1date; // this is the time difference in seconds
$diff = $diff / (60 * 60 * 24); // let's convert to days (60 seconds * 60 minutes * 24 hours = seconds in a day)

echo $diff;
?>[/code]
Copy linkTweet thisAlerts:
@ffurnaiSep 02.2005 — Oh, that makes sense.

Thanks!
×

Success!

Help @baltazhor200401 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...