0

hello sir..i am wondering how to calculate between two dates..here is my code:

<input type="text" name"date1" value="mm/dd/yyyy">
<input type="text" name"date2" value="mm/dd/yyyy">
<input type="submit" name"calculate" value="calculate">
<?php echo $answer;?>

example value of date1 is 09/26/2010
example value of date2 is 09/27/2010
so the answer should be 1 by subtracting date2 to date1

i hope you can help me with learning php..thanks daniweb -aizel

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by fredrickprem
1

Here is complete code what you need.
Hope it will help you.

<?php

	if(isset($_REQUEST['calculate']))
	{
		$temp1 = explode('/',$_REQUEST['date1']);
		$date1 = mktime(0,0,0,$temp1[0],$temp1[1],$temp1[2]);
		
		$temp2 = explode('/',$_REQUEST['date2']);
		$date2 = mktime(0,0,0,$temp2[0],$temp2[1],$temp2[2]);
		
		$dateDiff = $date2 - $date1;
		$fullDays = floor($dateDiff/(60*60*24));
		$fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
		$fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
		$answer = "Differernce between date2 and date1 is :  $fullDays days, $fullHours hours and $fullMinutes minutes.";
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form method="post">
date1 : <input name="date1" type="text" value="<?=$_REQUEST['date1'];?>">
date2 : <input name="date2" type="text" value="<?=$_REQUEST['date2'];?>">
<input type="submit" name="calculate" value="calculate">
<br />
<br />
<?php echo $answer;?>
</form>
</body>
</html>
0

it works like a charm :D thanks for the help sir..

2

I am not sir.. i am mam :) I think i should rename my profile name.

Votes + Comments
superb coading sir even i can calculate number of days from my birth
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.