2

I have a msg system in which the format is (Sent: 08:23:38 pm) and i want it like 1 min ago etc... this is what i got from here but its not helping me i m not able to fetch the timestamp from the db instead tried coping one of the value from column(timestamp) i.e 1467993620 so basically i want this to work in while loop and when i keep this in while loop i get error

plz help me for this

 $time = strtotime(date('h:i:s a','1467993620'));
    echo 'event happened '.humanTiming($time).' ago';

    function humanTiming ($time)
    {

        $time = time() - $time; // to get the time since that moment
        $time = ($time<1)? 1 : $time;
        $tokens = array (
            31536000 => 'year',
            2592000 => 'month',
            604800 => 'week',
            86400 => 'day',
            3600 => 'hour',
            60 => 'minute',
            1 => 'second'
        );

        foreach ($tokens as $unit => $text) {
            if ($time < $unit) continue;
            $numberOfUnits = floor($time / $unit);
            return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
        }

    }

i just posted query with while loop and the echo timestamp hope it helps you

     <?php
 $req2 = mysql_query('select pm.timestamp, pm.message, user.userID as userid, user.userName, user.userAddress from pm, user where pm.userID="'.$userID.'" and user.userID=pm.user1 order by pm.id2');
while($dn2 = mysql_fetch_array($req2))
{

?>
            Sent: <?php date_default_timezone_set('Asia/Kolkata'); echo date('h:i:s a',$dn2['timestamp']); ?> </small> </div>
            <br />
            <p> <?php echo $dn2['message']; ?> </p>

<?php
}
?>
Abhi Burk
  • 2,003
  • 1
  • 14
  • 21
  • Possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Henders Jul 08 '16 at 16:28
  • their is no error in the script the only thing i want is that how can i replace this script instead of timestamp format (Sent: 08:23:38 pm) to 1min ago my echo statement for this format (Sent: 08:23:38 pm) is like this : Sent: and i want to change it to 1min ago format how can i do that with this script sorry bit but im new to php so it will be very good if anyone plz answer in simple and easy way thanks in advance – Abhi Burk Jul 08 '16 at 16:34
  • i dont know what u are trying to say but i want format to be when user send message to another user then the time of the msg should look like 1min ago and etc etc... like fb have the script i posted is the same but that is not working in while loop and take example of this all comments they to have same format time ago format i hope u understand – Abhi Burk Jul 08 '16 at 17:20
  • Let's see : you want to display the difference between current time and the time when message was sent: "Sent 3 hours and 18 minutes and 55 seconds ago". – Jose Manuel Abarca Rodríguez Jul 08 '16 at 17:21
  • yeah you are right if its 60sec then it shud display 1 min if its 60 min then hour and if its 24 hours then 1day like that the above script i posted is the same but cant figured out i could i used that in my loop – Abhi Burk Jul 08 '16 at 17:24
  • Edited my answer with your new code. Check it out! – Jose Manuel Abarca Rodríguez Jul 08 '16 at 17:30
  • thanks mate it worked very much thank you to you and your time – Abhi Burk Jul 08 '16 at 17:40
  • Well, in that case, you can click the checkmark of the answer ^_º – Jose Manuel Abarca Rodríguez Jul 08 '16 at 17:42

2 Answers2

1
function time_elapsed_string($ptime)
{
$etime = time() - $ptime;

if ($etime < 1)
{
    return '0 seconds';
}

$a = array( 365 * 24 * 60 * 60  =>  'year',
             30 * 24 * 60 * 60  =>  'month',
                  24 * 60 * 60  =>  'day',
                       60 * 60  =>  'hour',
                            60  =>  'minute',
                             1  =>  'second'
            );
$a_plural = array( 'year'   => 'years',
                   'month'  => 'months',
                   'day'    => 'days',
                   'hour'   => 'hours',
                   'minute' => 'minutes',
                   'second' => 'seconds'
            );

foreach ($a as $secs => $str)
{
    $d = $etime / $secs;
    if ($d >= 1)
    {
        $r = round($d);
        return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
    }
}

}

Mattia
  • 124
  • 8
  • what should i replace from this above script i have column name timestamp which shows this format (Sent: 08:23:38 pm) – Abhi Burk Jul 08 '16 at 16:39
  • 1
    If I understand what you mean, my function replaces your function humanTiming($time) – Mattia Jul 08 '16 at 16:45
  • i m confuse a bit that where my column timestamp will be place in the script so that every time the user send msg then their should be like Sent 1min Ago – Abhi Burk Jul 08 '16 at 16:54
0

Try this format :

<?php
$time = strtotime(date('h:i:s a','1467993620'));
$time = $time - (1 * 60);                         // MINUS 60 SECONDS.
$date = date("h:i:s A", $time);                   // NOTICE THE "A".
echo "Sent: $date";
?>

Edit : calling humanTiming with your new code :

<?php
date_default_timezone_set('Asia/Kolkata');
echo "Sent: " . humanTiming( $dn2['timestamp'] ) . " ago.";
?>
  • yeah but i want that in loop thats the only thing i want whenever the user send msg their must be 1min ago format what shud i replce in this above script as i have timestamp column for simple format – Abhi Burk Jul 08 '16 at 16:45
  • nope i will explain see i have simple format which print this format (Sent: 08:23:38 pm) which is in while loop so instead of this format i want 1min ago format to be replace with that 1st one – Abhi Burk Jul 08 '16 at 16:50
  • still its the same format only second diffrence of yours and mine timestamp – Abhi Burk Jul 08 '16 at 16:58
  • no i dont want this type of "12:42:02 AM" format. and want to replace with 1min ago type format the above mine given script work fine but im confuse how should i use that in while loop to get 1min ago format – Abhi Burk Jul 08 '16 at 17:05
  • @AbhiBurk, post the while loop to see it. – Jose Manuel Abarca Rodríguez Jul 08 '16 at 17:05