doushi7314 2014-12-01 09:06
浏览 42

如何使用PHP中的表单找到DOB和现在的差异? [重复]

This question already has an answer here:

I wrote this code but i can output everything expect difference between there dates ?

what can i do to get difference between dates ?

out put
Your Birthday 1993/05/29
Today date is 2014/12/01
Differnce between days is

<?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>
</div>
  • 写回答

2条回答 默认 最新

  • dqk94069 2014-12-01 09:09
    关注
    <?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
    ", $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>";
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度