swoop180570 Posted August 23, 2006 Share Posted August 23, 2006 Hello php colleagues,I need a scripts what can calculate the difference between to dates on a users input.The user gives a start date and a end date. On input of end date the result must be the difference between the start and the end date. I know it can be done like this:[code]<?PHP $date_a = strtotime("2006-07-10");$date_b = strtotime("2006-07-23"); $days = ($date_b - $date_a) / (60*60*24); echo number_format($days); ?>[/code]This is just done by using to dates that are already known. I think the variables of $date_a and $date_b must be something like $date_a = $_POST[date1]; where date1 is the name of the input field.Can someone help me out here? I have a script already how it must be. It must be looking like this;[code]<?PHP// *** Defineren van POST input fields ***$date1 = $_POST[date1];$date2 = $_POST[date1];// *** Defineren van datum vandaag ***$datenow = date('d-m-Y');$result = "$datum1" - "$datum2";?><form id="form1" name="form1" method="post" action=""> Start Date: <label> <input name="date1" type="text" id="date1" value="<?PHP echo "$datenow"?>" size="8" maxlength="10" /> </label> End date: <input name="date2" type="text" id="date2" value="<?PHP echo "$datenow"?>" size="8" maxlength="10" /> Total day's: <?=$result?></form>[/code] Quote Link to comment Share on other sites More sharing options...
hax Posted August 23, 2006 Share Posted August 23, 2006 I wrote a simple example, try what I wrotetest.php (file)[code]<?phpecho "<form align='center' method=post action='t.php'>";echo "Data 1:";echo "<br /><input type=text name='data1'>";echo "<br /><br />Date 2:";echo "<br /><input type=text name='data2'>";echo "<br /><br />";echo "<input align='center' type=submit value='Submit'>";echo "</form>";?>[/code]t.php (file)[code]<?phpif ($REQUEST_METHOD=="POST") {$date_a = strtotime($_POST['data1']);$date_b = strtotime($_POST['data2']);$days = ($date_b - $date_a) / (60*60*24);echo number_format($days);} else {//have no postsecho "error";}?>[/code]It should work well. Quote Link to comment Share on other sites More sharing options...
swoop180570 Posted August 23, 2006 Author Share Posted August 23, 2006 Dear hax,I have been testing your example, but I got the "error" from your echo on line 11 when I input the dates and Submit it. I have tried serveral date stamps like dd-mm-yyyy or yyyy-mm-dd. In The Netherlands we use the date in this format (dd-mm-yyyy). Can you tell me what the correct input date is so I can test your example again. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2006 Share Posted August 23, 2006 seehttp://www.phpfreaks.com/forums/index.php/topic,105393.msg420995.html#msg420995 Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 23, 2006 Share Posted August 23, 2006 If your format will always be dd-mm-yyyy:[code]$startdate = explode("-", $_POST['data1']);$enddate = explode("-", $_POST['data2']);$days = floor((mktime(null, null, null, $enddate[1], $enddate[0], $enddate[2]) - mktime(null, null, null, $startdate[1], $startdate[0], $startdate[2])) / 86400);echo $days . " days difference between " . $_POST['data1'] . " and " . $_POST['data2'];[/code] Quote Link to comment Share on other sites More sharing options...
swoop180570 Posted August 24, 2006 Author Share Posted August 24, 2006 Thnx for your input hitman6003,I have replaced the previous code with your code and it is working fine.I'm verry happy with your solution. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 24, 2006 Share Posted August 24, 2006 Just make sure that you always have your date in that format or it won't work. You should also add in a check to make sure that the later date is set to $enddate, otherwise you will get strange numbers. Quote Link to comment Share on other sites More sharing options...
swoop180570 Posted August 25, 2006 Author Share Posted August 25, 2006 First of all I have set the date from today into the input fields by using the parameter $today = date ('d-m-Y'). The date of today will be added to both the input fields so the user knows how the input must be.I have also added a check into the script when the $days is smaller than 0 a error appears. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.