Geting Difference between two datae and time (Example 1) startPHP-022


		
<?php
   $DateTime1 = '2020-02-26 18:25:45';
   $DateTime2   = '03/26/20 18:47:00';

         ///////////////////////////////////////////
        function dateTimDiff($date1,$date2){
          $dif=array();
                $first = strtotime($date1);
                $second = strtotime($date2);
                $datediff = abs($first - $second);
                $dif['s'] = floor($datediff); 
                $dif['m'] = floor($datediff/(60)); //minute
                $dif['h'] = floor($datediff/(60*60)); //hour
                $dif['d'] = floor($datediff/(60*60*24));//day 
                $dif['M'] = floor($datediff/(60*60*24*30)); //Months
                $dif['y'] = floor($datediff/(60*60*24*30*365));//year             

           return $dif;     

        }/////////////////////////////////////////dateDiff($old,$new)   
   
   ?>
<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>StartPHP 022: Time and Date Difference Code 1</title>
    </head>
    <body>
        <h1>Time Difference</h1>

        <span style="font-size:1m">First date and time: <?php echo $DateTime1?></span><br/>
        <span style="font-size:1m">Second date and time: <?php echo $DateTime2?></span><br/>
       Seconds: <?php echo  dateTimDiff($DateTime1,$DateTime2)['s']?><br/>
       Hours: <?php echo  dateTimDiff($DateTime1,$DateTime2)['h']?><br/>
       Minutes: <?php echo  dateTimDiff($DateTime1,$DateTime2)['m']?><br/>
       Days: <?php echo  dateTimDiff($DateTime1,$DateTime2)['d']?><br/>  
       Months: <?php echo  dateTimDiff($DateTime1,$DateTime2)['M']?><br/>        
    </body>
</html>