Jump to content

difference between two dates on users input


swoop180570

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]


Link to comment
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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.