Jump to content

Archived

This topic is now archived and is closed to further replies.

swoop180570

difference between two dates on users input

Recommended Posts

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]


Share this post


Link to post
Share on other sites
I wrote a simple example, try what I wrote

test.php (file)
[code]<?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>";
?>[/code]

t.php (file)
[code]<?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";
}
?>[/code]

It should work well.

Share this post


Link to post
Share on other sites
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.
  

Share this post


Link to post
Share on other sites
see

http://www.phpfreaks.com/forums/index.php/topic,105393.msg420995.html#msg420995

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.