如何使用 PHP 中的表单找到 DOB 和现在之间的差异


how to find difference between DOB and present day using forms in php?

我写了这段代码,但我可以输出所有期望日期之间的差异?

我该怎么做才能获得日期之间的差异?


输出你的生日 1993/05/29
今天日期是 2014/12/01
天之间的差异是

<?php
if(isset($_POST['submit']))
  {
  $brt_dat = $_POST['brt_dat'];
  $tdy_dat = date("Y/m/d");
  echo "Your Birthday $brt_dat<br>";
  echo "Today date is $tdy_dat<br>";
  $diff = date_diff($tdy_date,$brt_day);
  echo "Differnce between days is $diff";
  }
  ?>`
 <html>
 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  <input type="date" name="brt_dat" placeholder="(YYYY/MM/DD)" >
  <input type="submit" name="submit"  value="calculate">
  </form>
  </html>

使用日期时间:

  $brt_day = new DateTime('1993/05/29');//in your example use $_POST['brt_dat']
  $tdy_dat = new DateTime();
  $interval = $brt_day->diff($tdy_dat);
  echo $interval->format('%a days');

要显示两个日期之间的所有日期:

$brt_day = new DateTime('1993/05/29');
$tdy_dat = new DateTime();
while($brt_day<$tdy_dat){
    echo $brt_day->format('Y/m/d').'<br />';
    $brt_day->modify('+1day');
}
<?php 
$brt_dat = $_POST['brt_dat'];
$tdy_dat = date("Y/m/d");
echo "Your Birthday $brt_dat<br>";
echo "Today date is $tdy_dat<br>";
$diff = date_diff($tdy_date,$brt_day);
$date1 = date('Y-m-d',strtotime($brt_dat));
$date2 = date('Y-m-d',strtotime($tdy_dat));
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
printf("Differnce between days is %d years, %d months, %d days'n", $years, $months, $days);
//As of PHP version >= 5.3, You can use this code to display all dates between two
$begin = new DateTime($date1);
$end = new DateTime($date2);
$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);
foreach($daterange as $date){
    echo $date->format("Y-m-d") . "<br>";
}
?>