PHP Help with time since x happened
6 posts
Page 1 of 1
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:
![Image]()
Any help would be greatly appreciated
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
that's hours:minutes am/pm$timeadded = date("h:ia");

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!
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.
smashapps wrote:So you only store time in database? Not date as well?Code: Select all$timeadded = date("h:ia");
CodenStuff wrote:Nope, it's just your sick and dirty mind. You sick twisted warped little pervo![]()
Now I feel like an idiot, lol.
Forget the code I posted. I was using timestamp in the database which is the date/time
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!
Would this help?
http://php.net/manual/en/function.time.php#89415
Code: Select all
Lots of time function examples here that should help you achieve your goal:<?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);
?>
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.
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.

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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023