Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • PHP Getting Time Elapsed Per Last Comment

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 149
    Comment on it

    Passing date to the function and returning the time passed or elapsed when it was commented/posted. It is very useful when you have comment section in your website and you want to show how earlier comment was posted.


    /**
    * Creating the date difference in the correct text format.
    *
    * @param  date $recordCreated.
    * @return string $finalResult Response.
    */
    function getTimeDifferenceFormat($recordCreated) {
        $date = date('Y-m-d H:i:s');
        $datetime1 = new DateTime($date);
        $datetime2 = new DateTime($recordCreated);
        $interval = $datetime1->diff($datetime2);
        $dateFormat = $interval->format('%a:%h:%i:%s');
        $data = explode(':', $dateFormat);
        $finalResult = "";
        if (!empty($data)) {
            $days = $data[0];
            $hours = $data[1];
            $minutes = $data[2];
            $seconds = $data[3];
            if ($days == 0) {
                if ($hours == 0) {
                    if ($minutes == 0) {
                        $finalResult = "few seconds ago.";
                    } else {
                        $finalResult = ($minutes == 1) ? $minutes . " minute ago." : $minutes . " minutes ago.";
                    }
                } else {
                    $finalResult = ($hours == 1) ? $hours . " hour ago." : $hours . " hours ago.";
                }
            } else {
                $finalResult = ($days == 1) ? $days . " day ago." : $days . " days ago.";
            }
        } else {
            $finalResult = "problem";
        }
        return $finalResult;
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: