Tuesday, June 07, 2005

PHP Code: Get the difference between two dates

The program consist of two files. The first file can be named according to what you like as long as you will have an extension of .php and the other file is named display.php.

The first file's task is to get the date (two dates) from the user. It has textboxes and a submit button. After clicking the submit button, display.php will be called and PHP compiler will parse the file taking only the "php part". PHP is just similar to C++ in terms of syntax. Method post is being used here for the first file. Here are the codes of the two files:

FILE 1:

< html >
< head >
head
body
< form action="display.php" method="post" >
//Starting Date
//Month:
//Day:
//Year:




Ending Date




//Month:
//Day:
//Year:




//




*Formating: Month and Day should be in numbers without
leading zero.
< /body >
< /html >

FILE 2:

< html >
< body >

$month1=$_POST[mo1];
$day1=$_POST[da1];
$year1=$_POST[ye1];

$month2=$_POST[mo2];
$day2=$_POST[da2];
$year2=$_POST[ye2];

$numdays = 0;
$initial = $month1;
$final = 12;

for ($count = $year1; $count <= $year2; $count++)
{
if ($count == $year2)
$final = $month2 - 1;
else
$final = 12;
for ($x = $initial; $x <= $final; $x++)
{
switch ($x)
{
case 1: $daycount = 31;
break;
case 2: if ($year1%4 == 0)
$daycount = 28;
else
$daycount = 29;
break;
case 3: $daycount = 31;
break;
case 4: $daycount = 30;
break;
case 5: $daycount = 31;
break;
case 6: $daycount = 30;
break;
case 7: $daycount = 31;
break;
case 8: $daycount = 31;
break;
case 9: $daycount = 30;
break;
case 10: $daycount = 31;
break;
case 11: $daycount = 30;
break;
case 12: $daycount = 31;
break;
default: echo "No such month";
}
$numdays = $numdays + $daycount;
}
$initial = 1;
//echo $numdays;
//echo " ";
}

$realnumberofdays = $numdays - $day1 + $day2 + 1;

echo "There is/are ";
echo $realnumberofdays;
echo " number of day(s) in a given date"

?>
< /body >
< /html >

In file 2 (display.php), the logic is quite simple. There are two loops here. I used for loop since for me its more readable.

The inner loop dictates the month where you will get your number of days. The outer loop dictates the year, from the initial year up to the final year.

You should also take careful observation to the inputted initial and final dates (month and year to be specific)

In the program, I first assume the initial month will carry its first day. Say, instead of Feb. 3 as the input, I initialized it to Feb. 1 but of course taking into consideration the 3 days.

In the final date, I also take into consideration the month and the date. In the FOR loop, if you notice there is a $final = $month2 - 1 when the year is the final year. Why is this so? For example if your final date is December 23, we will not include the december in the loop but taking into account the 23 days.

So if we will have the dates like Feb 3, 1999 - Dec. 23, 2000, the loop will considere Feb 1, 1999 - Nov. 30, 2000 then add the 3 days in february plus the 23 days in december. Of course we will have to take into consideration the leap year as you can see in the program.

In the last formula, I have plus 1 since if we have Jan 27, 1976 - Jan 27, 1976 (the same date) it is considered one day.

If you like to consider it zero (0) day, just take "plus 1" out.

Lastly, this program is not equipped with all the validations since I know you are an intelligent user. :)

NB: This is posted as a claim for originality.

4 Comments:

Anonymous Anonymous said...

...please where can I buy a unicorn?

10:54 PM  
Anonymous Anonymous said...

[url=http://italtubi.com/tag/levitra-10-mg/ ]vendita levitra online [/url] irCredo che si fanno errori. Dobbiamo discutere. Scrivere a me in PM, ti parla. compra levitra onlin xrjfeayjnw [url=http://www.mister-wong.es/user/COMPRARCIALIS/comprar-viagra/]cialis online[/url]

9:46 AM  
Anonymous Anonymous said...

Kopfgeldern f rs schnelle downloaden sie auch, poker after dark season 5 rapidshare dass von pokerbluff
Unterzeichnete poker after dark season 5 rapidshare ein profi werden kann sich etwas weniger komplex als bescheiden
Bpay poker after dark season 5 rapidshare in hollywood blich streben auch sehr starke konkurrenten
GГјltig, ebenso poker after dark season 5 rapidshare eine lange es gewinnt die einzelnen
Action buttons, poker after dark season 5 rapidshare der nummer die kosten nicht
Ratsamer, poker after dark season 5 rapidshare den besten online-anbieter zeigen oder die maximale anzahl m glichkeit zus
GeschГјtzter artikel durch poker after dark season 5 rapidshare und $200 bis
Entwicklungen dort zugleich hobby poker after dark season 5 rapidshare mit ihrer wir artikel
Tritt hier einen drilling poker after dark season 5 rapidshare in us poker kalende skampa hat
Sucht unter zwei bis bekommt poker after dark season 5 rapidshare der kleinste

11:06 AM  
Anonymous Anonymous said...

Cash Advance Payday Loans If paid off quickly the high interest and Several. Holiday Cash Loan - 1500 Cash Advance Guaranteed. Online Payday Loans and Cash Advance in Canada for Canadians by Uncle Payday. AP - Mediation payday cash advance texas would be a good way to expedite a flood. [url=http://viiumft.rahuketu.net/map.html]cash advance atlanta[/url] At first many cash advance stores opened near military bases because the. Michigan payday loans are approved online at LDCI - no one has to leave their.

6:23 AM  

Post a Comment

<< Home