• Top Members
    Reps
    Posts
  • 834 Replies
    2585 Replies
  • 716 Replies
    2025 Replies
  • 331 Replies
    1928 Replies

PHP Help with time since x happened

User avatar
smashapps
Coding Guru
Posts: 960

PHP Help with time since x happened

Tue Nov 01, 2016 8:02 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
Posts: 4361

Re: PHP Help with time since x happened

Tue Nov 01, 2016 3:52 pm

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.

User avatar
Filip
Coding Guru
Posts: 833

Re: PHP Help with time since x happened

Tue Nov 01, 2016 5:09 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
Posts: 960

Re: PHP Help with time since x happened

Wed Nov 02, 2016 2:46 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
Posts: 4361

Re: PHP Help with time since x happened

Wed Nov 02, 2016 4:47 pm

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
Posts: 960

Re: PHP Help with time since x happened

Wed Nov 02, 2016 5:13 pm

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!

Post Reply

Return to “Help & Support”