Archi Patel
Archi Patel

Reputation: 63

How to display time difference in php

I want to display this data as time difference like 2 mins ago, 2 days ago.

My code doesnt show the right time and also it displays the same time in all posted data.

 <?php

     $conn = new mysqli("localhost","root","","blog_samples");
     $sql="UPDATE comments SET status=1 WHERE status=0"; 
     $result=mysqli_query($conn, $sql);
     try{
       $con = new PDO("mysql:host=localhost;dbname=blog_samples",'root','');
       $sth = $con->prepare( "SELECT * FROM `comments` ORDER BY id DESC limit 5");
       $sth->setFetchMode(PDO:: FETCH_OBJ);
       $sth->execute();
       while($row=$sth->fetch()){   
         ?>
          <li>
             <a href="javascript:void(0)">
             <span class="label label-primary"><i class="fa fa-user"></i></span>
             <span class="message"> New POST</span>
             <span class="time"><?php
           $tim = $row->samay;
           $date1 = strtotime($tim);
           $date2 = strtotime(date('Y-m-d H:i:s'));
           $seconds_diff = $date2 - $date1;

           echo round(abs($seconds_diff) / 60,2). " mins ago";
           ?></span>
              </a>
           </li>

          <?php } }

    catch(PDOEception $e){
          echo "Error: ". $e->getMessage();
             }

           ?>

Upvotes: 1

Views: 131

Answers (3)

Archi Patel
Archi Patel

Reputation: 63

This is display data code

 <?php
                                         include('functions.php');
                                         $conn = new mysqli("localhost","root","","blog_samples");
                                         $sql="UPDATE comments SET status=1 WHERE status=0"; 
$result=mysqli_query($conn, $sql);
                                try
                                    {

                                    $con = new PDO("mysql:host=localhost;dbname=blog_samples",'root','');

                                    $sth = $con->prepare( "SELECT * FROM `comments` ORDER BY id DESC limit 5");



                                    $sth->setFetchMode(PDO:: FETCH_OBJ);

                                    $sth->execute();

                                    while($row=$sth->fetch())
                                    {   
                                    ?>
                                        <li>
                                            <a href="javascript:void(0)">
                                                <span class="label label-primary"><i class="fa fa-user"></i></span>
                                                <span class="message"> New POST</span>
                                                <span class="time"><?php
                                            echo timeAgo(date("Y-m-d H:i:s"));

?>
                                               </span>
                                            </a>
                                        </li>


                                       <?php } }

                            catch(PDOEception $e){
                            echo "Error: ". $e->getMessage();
                                }

                            ?>

and this is functions.php

<?php

function timeAgo($time_ago) {
        $time_ago = strtotime($time_ago);
        $cur_time = time();
        $time_elapsed = $cur_time - $time_ago;
        $seconds = $time_elapsed;
        $minutes = round($time_elapsed / 60);
        $hours = round($time_elapsed / 3600);
        $days = round($time_elapsed / 86400);
        $weeks = round($time_elapsed / 604800);
        $months = round($time_elapsed / 2600640);
        $years = round($time_elapsed / 31207680);
// Seconds
        if ($seconds <= 60) {
            return "$seconds seconds ago";
        } //Minutes
        else if ($minutes <= 60) {
            if ($minutes == 1) {
                return "one minute ago";
            } else {
                return "$minutes minutes ago";
            }
        } //Hours
        else if ($hours <= 24) {
            if ($hours == 1) {
                return "an hour ago";
            } else {
                return "$hours hours ago";
            }
        } //Days
        else if ($days <= 7) {
            if ($days == 1) {
                return "yesterday";
            } else {
                return "$days days ago";
            }
        } //Weeks
        else if ($weeks <= 4.3) {
            if ($weeks == 1) {
                return "a week ago";
            } else {
                return "$weeks weeks ago";
            }
        } //Months
        else if ($months <= 12) {
            if ($months == 1) {
                return "a month ago";
            } else {
                return "$months months ago";
            }
        } //Years
        else {
            if ($years == 1) {
                return "one year ago";
            } else {
                return "$years years ago";
            }
        }
    }
?>

all time ago are same displayed same @Akshay komarla

Upvotes: 0

Milan Chheda
Milan Chheda

Reputation: 8249

Here is function that will return timeago:

<?php
function get_timeago( $ptime )
{
    $estimate_time = time() - $ptime;

    if( $estimate_time < 1 )
    {
        return 'less than 1 second ago';
    }

    $condition = array( 
                12 * 30 * 24 * 60 * 60  =>  'year',
                30 * 24 * 60 * 60       =>  'month',
                24 * 60 * 60            =>  'day',
                60 * 60                 =>  'hour',
                60                      =>  'minute',
                1                       =>  'second'
    );

    foreach( $condition as $secs => $str )
    {
        $d = $estimate_time / $secs;

        if( $d >= 1 )
        {
            $r = round( $d );
            return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
        }
    }
}
?>

Upvotes: 2

Akshay komarla
Akshay komarla

Reputation: 744

use this function

function timeAgo($time_ago) {
        $time_ago = strtotime($time_ago);
        $cur_time = time();
        $time_elapsed = $cur_time - $time_ago;
        $seconds = $time_elapsed;
        $minutes = round($time_elapsed / 60);
        $hours = round($time_elapsed / 3600);
        $days = round($time_elapsed / 86400);
        $weeks = round($time_elapsed / 604800);
        $months = round($time_elapsed / 2600640);
        $years = round($time_elapsed / 31207680);
// Seconds
        if ($seconds <= 60) {
            return "$seconds seconds ago";
        } //Minutes
        else if ($minutes <= 60) {
            if ($minutes == 1) {
                return "one minute ago";
            } else {
                return "$minutes minutes ago";
            }
        } //Hours
        else if ($hours <= 24) {
            if ($hours == 1) {
                return "an hour ago";
            } else {
                return "$hours hours ago";
            }
        } //Days
        else if ($days <= 7) {
            if ($days == 1) {
                return "yesterday";
            } else {
                return "$days days ago";
            }
        } //Weeks
        else if ($weeks <= 4.3) {
            if ($weeks == 1) {
                return "a week ago";
            } else {
                return "$weeks weeks ago";
            }
        } //Months
        else if ($months <= 12) {
            if ($months == 1) {
                return "a month ago";
            } else {
                return "$months months ago";
            }
        } //Years
        else {
            if ($years == 1) {
                return "one year ago";
            } else {
                return "$years years ago";
            }
        }
    }

Upvotes: 1

Related Questions