const getDaysDiffBetweenDates = (dateInitial, dateFinal) =>
(dateFinal - dateInitial) / (1000 * 3600 * 24);
// Example
getDaysDiffBetweenDates(new Date('2017-12-13'), new Date('2017-12-22')); // 9
const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)
dayDif(new Date("2020-10-21"), new Date("2021-10-22"))
// Result: 366
<?php
$date2='2022-08-1 2:10:00';
date_default_timezone_set('Asia/Karachi');
$date1 = date("Y-m-d H:i:s");
$timestamp1 = strtotime($date1);
$timestamp2 = strtotime($date2);
$posted_time_hours = round((abs($timestamp2 - $timestamp1))/(60*60)) ;
if(($posted_time_hours) >= 24){
$posted_time_hours = round($posted_time_hours/24) . " day(s) ago";
}
else{
$posted_time_hours = $posted_time_hours . " hour(s) ago";
}
echo $posted_time_hours;
?>