PHP Help with time since x happened

6 posts Page 1 of 1
Contributors
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Hey guys,

Been awhile I know, but I am working on a new project. It is a ticketing system.
One of the features on the site is a chat box, you can enter a message and click send and the message will show up with your name however I want to know if anyone knows of a function i can put in to be able to show for example "Posted 5 minutes ago" or "Posted 1 week, 2 days ago" in the chat.

I know there are a few examples hanging around online but I've had trouble getting them to work.

The way I generate the time in my mysql database is with this:
Code: Select all
$timeadded = date("h:ia");
that's hours:minutes am/pm

Image

Any help would be greatly appreciated :)
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Are you actually saving the time to the database as "3:45am" or is it stored as a timestamp and you're outputting it as "3:45am"?
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
Filip
Coding Guru
Coding Guru
Posts: 833
Joined: Wed Jan 05, 2011 3:59 pm

smashapps wrote:
Code: Select all
$timeadded = date("h:ia");
So you only store time in database? Not date as well?
CodenStuff wrote:
Nope, it's just your sick and dirty mind. You sick twisted warped little pervo :D
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

Now I feel like an idiot, lol.

Forget the code I posted. I was using timestamp in the database which is the date/time
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Would this help?
Code: Select all
<?php
function time_elapsed($secs){
    $bit = array(
        ' year'        => $secs / 31556926 % 12,
        ' week'        => $secs / 604800 % 52,
        ' day'        => $secs / 86400 % 7,
        ' hour'        => $secs / 3600 % 24,
        ' min'    => $secs / 60 % 60,
        ' sec'    => $secs % 60
        );
        
    foreach($bit as $k => $v){
        if($v > 1)$ret[] = $v . $k . 's';
        if($v == 1)$ret[] = $v . $k;
        }
    array_splice($ret, count($ret)-1, 0, 'and');
    $ret[] = 'ago.';
    
    return join(' ', $ret);
    }

//This would be your time from database
$time_from_db = 1478101347;

echo 'Posted ' . time_elapsed(time()-$time_from_db);
?>
Lots of time function examples here that should help you achieve your goal:
http://php.net/manual/en/function.time.php#89415
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
smashapps
Coding Guru
Coding Guru
Posts: 961
Joined: Tue Apr 05, 2011 8:41 am

I changed the way my time is stored in the database and used the function you posted and it is all working great now!

Thanks heaps for that, I think I tried a function similar the other day but I was using the incorrect timestamp. Simple things get so easily overlooked.

:)
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
6 posts Page 1 of 1
Return to “Help & Support”