Wednesday, June 13, 2012

Calculate time difference using PHP


 function get_time_difference_php($created_time)
 {
        date_default_timezone_set('Asia/Calcutta'); //Change as per your default time
        $str = strtotime($created_time);
        $today = strtotime(date('Y-m-d H:i:s'));
        
        // It returns the time difference in Seconds...
        $time_differnce = $today-$str;
        
        // To Calculate the time difference in Years...
        $years = 60*60*24*365;
        
        // To Calculate the time difference in Months...
        $months = 60*60*24*30;
        
        // To Calculate the time difference in Days...
        $days = 60*60*24;
        
        // To Calculate the time difference in Hours...
        $hours = 60*60;
        
        // To Calculate the time difference in Minutes...
        $minutes = 60;

        if(intval($time_differnce/$years) > 1)
        {
            return intval($time_differnce/$years)." years ago";
        }else if(intval($time_differnce/$years) > 0)
        {
            return intval($time_differnce/$years)." year ago";
        }else if(intval($time_differnce/$months) > 1)
        {
            return intval($time_differnce/$months)." months ago";
        }else if(intval(($time_differnce/$months)) > 0)
        {
            return intval(($time_differnce/$months))." month ago";
        }else if(intval(($time_differnce/$days)) > 1)
        {
            return intval(($time_differnce/$days))." days ago";
        }else if (intval(($time_differnce/$days)) > 0) 
        {
            return intval(($time_differnce/$days))." day ago";
        }else if (intval(($time_differnce/$hours)) > 1) 
        {
            return intval(($time_differnce/$hours))." hours ago";
        }else if (intval(($time_differnce/$hours)) > 0) 
        {
            return intval(($time_differnce/$hours))." hour ago";
        }else if (intval(($time_differnce/$minutes)) > 1) 
        {
            return intval(($time_differnce/$minutes))." minutes ago";
        }else if (intval(($time_differnce/$minutes)) > 0) 
        {
            return intval(($time_differnce/$minutes))." minute ago";
        }else if (intval(($time_differnce)) > 1) 
        {
            return intval(($time_differnce))." seconds ago";
        }else
        {
            return "few seconds ago";
        }
  }

Tuesday, June 5, 2012

Difference between current date and past date using MySQL


Below is the function to get the time difference between Current date and the past date using MySQL. Mainly it can be used where we want to display the time gap between the registration of the user.


function getTimeDifference($created_since)
{
            $datediffquery = mysql_query  ("SELECT CASE WHEN TIMESTAMPDIFF(YEAR ,'".$created_since."', NOW()) > 1 THEN CONCAT(TIMESTAMPDIFF(YEAR ,'".$created_since."', NOW()), ' years ago')
                                                WHEN TIMESTAMPDIFF(YEAR ,'". $created_since."', NOW()) > 0 THEN CONCAT(TIMESTAMPDIFF(YEAR ,'". $created_since ."', NOW()), ' year ago')
                                                WHEN TIMESTAMPDIFF(MONTH ,'".$created_since."', NOW()) > 1 THEN CONCAT(TIMESTAMPDIFF(MONTH ,'".$created_since."', NOW()), ' months ago')
                                                WHEN TIMESTAMPDIFF(MONTH ,'".$created_since."', NOW()) > 0 THEN CONCAT(TIMESTAMPDIFF(MONTH ,'".$created_since."', NOW()), ' month ago')
                                                WHEN TIMESTAMPDIFF(DAY ,'".$created_since."', NOW()) > 1 THEN CONCAT(TIMESTAMPDIFF(DAY ,'".$created_since."', NOW()), ' days ago')
                                                WHEN TIMESTAMPDIFF(DAY ,'".$created_since."', NOW()) > 0 THEN CONCAT(TIMESTAMPDIFF(DAY ,'".$created_since."', NOW()), ' day ago')
                                                WHEN TIMESTAMPDIFF(HOUR ,'".$created_since."', NOW()) > 1 THEN CONCAT(TIMESTAMPDIFF(HOUR ,'".$created_since."', NOW()), ' hours ago')
                                                WHEN TIMESTAMPDIFF(HOUR ,'".$created_since."', NOW()) > 0 THEN CONCAT(TIMESTAMPDIFF(HOUR ,'".$created_since."', NOW()), ' hour ago')
                                                WHEN TIMESTAMPDIFF(MINUTE ,'".$created_since."', NOW()) > 1 THEN CONCAT(TIMESTAMPDIFF(MINUTE ,'".$created_since."', NOW()), ' minutes ago')
                                                WHEN TIMESTAMPDIFF(MINUTE ,'".$created_since."', NOW()) > 0 THEN CONCAT(TIMESTAMPDIFF(MINUTE ,'".$created_since."', NOW()), ' minute ago')
                                                WHEN TIMESTAMPDIFF(SECOND ,'".$created_since."', NOW()) > 1 THEN CONCAT(TIMESTAMPDIFF(SECOND ,'".$created_since."', NOW()), ' seconds ago')
                                            END
                                        AS total_days");
            
            $row = mysql_fetch_assoc($datediffquery);
            
            return $row['total_days'];
        }