Jump to content


Photo

difference between two dates on users input


  • Please log in to reply
7 replies to this topic

#1 swoop180570

swoop180570
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 August 2006 - 10:18 AM

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:

<?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);
 
?>

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;

<?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>




#2 hax

hax
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 August 2006 - 10:40 AM

I wrote a simple example, try what I wrote

test.php (file)
<?php
echo "<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>";
?>

t.php (file)
<?php
if ($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 posts
echo "error";
}
?>

It should work well.



#3 swoop180570

swoop180570
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 23 August 2006 - 08:10 PM

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.
  

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 23 August 2006 - 10:17 PM

see

http://www.phpfreaks....html#msg420995
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 23 August 2006 - 10:20 PM

If your format will always be dd-mm-yyyy:

$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'];


#6 swoop180570

swoop180570
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 24 August 2006 - 07:44 AM

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.


#7 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 24 August 2006 - 01:32 PM

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.

#8 swoop180570

swoop180570
  • Members
  • Pip
  • Newbie
  • 4 posts

Posted 25 August 2006 - 07:31 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users