-2

What I am trying to achieve is basically the user will enter 2 dates and get back the difference in days. I have seen other related questions in SO but none answer my question.

Following is my code:

<html>
<head>
<?php
$startdate = $_POST['startdate']; // starting date
$enddate = $_POST['enddate']; // end date
$now = strtotime($startdate);
$your_date = strtotime($enddate);
$datediff = $your_date - $now;
$number = floor($datediff/(60*60*24));
?>

</head>
<body>
<form name="date_form" action="" method="POST"">
<input type="text" name="startdate" value=""/>
<input type="text" name="enddate" value=""/>
<input type="submit" name="submit_start" value="Submit" />
</form>
</body>
</html>

Please help in pointing what all changes I should make. Thanks in advance.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Caffeine Coder
  • 1,869
  • 1
  • 17
  • 35

2 Answers2

0

You can Use DateTime() and DateInterval() for date math. Much easier to do.


but if you want to use date() :

date() function get 2nd parameter as time stamp and returns the date : 1970-1-1 00:00:00 GMT + given time stamp , pay attention to GMT part. it means if your time zone is +2, then echo date("Y-m-d H:i:s",0) returns 1970-1-1 2:00:00 , not 1970-1-1 00:00:00.

So, the result that you are getting, is (real different) + 2 hours(your time zone) . if you insist of getting the result using date(), you must change the last lines to:

$number = floor( ( $datediff-$YOUR_TIME_ZONE ) /(60*60*24));

Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
0

Why you making the for loop?

$number = floor($datediff/(60*60*24));

$number is the diffrence in days.

OfirH
  • 651
  • 1
  • 8
  • 19