This question already has an answer here:
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?
</div>
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>';
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
];