Need a script that will calculate the difference between two dates<

liunx

Guest
Hello, I've looked on hotscripts and google and couldn't find one.

I'm looking for a script that will a) allow me to type in two dates in mm/dd/yy format and telling me the difference in days between the two dates, or b) having a drop down menu for the month, date, and year for two different dates and having it tell me the difference in number of days between the two dates. THe closest I could find on hotscripts was a script that would allow me to enter the date and a number of days and it would either add it or subtract it from the date entered in the drop down menues.

Any help is greatly appreciated.

JoshThis is how i would do it in the following format
mm/dd/yyyy



$date_one = explode("/",$dateone);
$date_two = explode("/",$datetwo);

$dateOne = mktime(0,0,0,$date_one['0'],date_one['1'],date_one['2']);

$dateTwo = mktime(0,0,0,$date_two['0'],date_two['1'],date_two['2']);

//from here is basically work out which is largests

if ($dateOne < $dateTwo) {
$diff = ($dateOne-$dateTwo) / 3600 / 24;
} else {
$diff = ($dateTwo-$dateOne) / 3600 / 24;
}

echo $diff;



Its untested but it gives a basic view of telling how many days differenceHmm...How would I use this script? Would I need to make some sort of form? Thanks for the help.The very _basic_ way of doing it:

A form in some html page:

<form method="post" action="datediff.php">
Date one: <input type="text" name="dateone" value="mm/dd/yyyy" /><br />
Date two: <input type="text" name="datetwo" value="mm/dd/yyyy" /><br />
<input type="submit" />
</form>


The datediff.php file:

$date_one = $_POST['dateone'];
$date_two = $_POST['datetwo'];

$date_one = explode("/",$date_one);
$date_two = explode("/",$date_two);

$dateOne = mktime(0, 0, 0, $date_one[0], $date_one[1], $date_one[2]);

$dateTwo = mktime(0, 0, 0, $date_two[0], $date_two[1], $date_two[2]);

//from here is basically work out which is largests

if ($dateOne > $dateTwo) {
$diff = ($dateOne-$dateTwo) / 3600 / 24;
} else {
$diff = ($dateTwo-$dateOne) / 3600 / 24;
}

echo $diff;



Try that. I didn't try the script planetsim provided, I'm assuming that it works.

Edit:
Well.. Fixed some of the mistakes in the code.*nevermind* i can't read! :PIt worked, thanks rydberg and planetism.in theory you caould also change the dates to a timestamp and subtract both of them and then get the answer in days. but if that way works then good for you.Originally posted by scoutt
in theory you caould also change the dates to a timestamp and subtract both of them and then get the answer in days. but if that way works then good for you.

Hmm isn't that what the script is doing? Not sure what you mean by subtracting both of them though.. From what? The current code subtracts the smaller timestamp from the greater timestamp.by using strtotime()Read Internet Standard RFC 3339 (Google for it) for a universally accepted format for expressing date and time, which avoids the ambiguity in dates like 01/02/03.
 
Back
Top