Issue on Converting to minutes and seconds from date [duplicate]
Hello what im doing wrong converting to minutes and seconds?
//Convert to date $startdatesrt="2020-11-13 12:00:00"; //example start date $startdate=strtotime($startdatesrt); $enddatesrt="2020-11-31 10:00:00"; //example end date $enddate=strtotime($enddatesrt); $currentDate = time(); //Calculate difference $diff=$enddate-$currentDate; $days=floor($diff/(60*60*24)); $hours=round(($diff-$days*60*60*24)/(60*60)); $minutes = floor($diff-$hours/60); $seconds = floor($diff-$minutes/60); echo "$days days $hours hours $minutes minutes $seconds remain<br />";
output: 16 days 19 hours 1450554 minutes 1426379 remain
The problem with the minutes and the seconds is that you are dividing 19 hours / 60 = 0,316666667, so the $minutes are the original $diff less 0,316..
If you want to do it in the same way that you were doing it:
$SECOND = 1; $MINUTE = 60 * $SECOND; $HOUR = 60 * $MINUTE; $DAYS = 24 * $HOUR; //Calculate difference $diff=strtotime("2020-11-31 10:00:00")-time(); $days = floor($diff / $DAYS); $diff = $diff - $days * $DAYS; $hours = floor($diff / $HOUR); $diff = $diff - $hours * $HOUR; $minutes = floor($diff / $MINUTE); $seconds = $diff - $minutes * $MINUTE; echo "$days days $hours hours $minutes minutes $seconds seconds remain<br />";