PHP计算日期,小时和分钟时出错?

时间:2019-03-20 14:30:37

标签: php html

我的问题是我遇到了问题

  

注意:尝试获取非对象的属性“ 7444”   C:\ xampp \ htdocs \ ex2.php在第22行

天过去了。 因此它可以计算时间,日期等,但它不想显示在我的页面上。

          <html>
          <link rel="stylesheet" href="8ex2.css">
          <body>
          <div class="inside">
          Welcome <?php echo $_POST["name"]; ?><br>

          Your Birthday is: <?php echo $_POST["dob"]; ?><br>

          <?php
              $date = $_POST["dob"];
              $now = time();
              $birthDay = strtotime($date);

              $newBDate = date("d-M-Y", strtotime($date));

              $todaysDate=getdate();
              $difference = $now - $birthDay;
              $minutes = floor($difference/ (60));
              $hours = floor($difference/ (60*60));
              $days = floor($difference / (60*60*24));
              $weeks = floor($difference / (60*60*24*7));

              echo $difference->$days.' days have passed.<br>';
              echo $difference->$hours.' hours have passed.<br>';
              echo $difference->$minutes.' minutes have passed.<br>';

                ?>
          </div>
          </body>
          </html>

我不了解我在PHP中做错了什么吗? 谢谢!

2 个答案:

答案 0 :(得分:2)

为什么使用$difference->$days?该变量称为$days

就做

echo $days.' days have passed.<br>';
echo $hours.' hours have passed.<br>';
echo $minutes.' minutes have passed.<br>';

答案 1 :(得分:0)

您可以在此处使用DateInterval类中的本地DateTime

// Create the initial date object
$datetime1 = new DateTime($_POST["dob"]);

// Compare it to now
$interval = $datetime1->diff(new DateTime());

$result = [
    'days' => $interval->days,
    'hours' => $interval -> days * 60 + $interval->h,
    'minutes' => $interval -> days * 60 + $interval->h * 24 + $interval->m
];