1

My problem is that I get the issue

Notice: Trying to get property '7444' of non-object in C:\xampp\htdocs\ex2.php on line 22

days have passed. So it calculates the time, day and etc but it doesn't want to show on my page.

          <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>

Which I don't understand is there something that I am doing incorrectly in my PHP?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ugur Denli
  • 23
  • 5

2 Answers2

2

Why the $difference->$days? The variable is called $days.

Just do

echo $days.' days have passed.<br>';
echo $hours.' hours have passed.<br>';
echo $minutes.' minutes have passed.<br>';
JensV
  • 3,997
  • 2
  • 19
  • 43
0

You can use the native DateInterval from the DateTime class here

// 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
];
GuCier
  • 6,919
  • 1
  • 29
  • 36