i want to know the logic used behind displaying the time stamp like x minutes ago or x hours ago and after few hours showing the exact time like posted at 7:57 pm like they do it in mail sites, and also twitter etc.
how do i know when to show the seconds/minutes/hours range and when to show the actual time in am/pm format? please suggest the business logic used to achieve this.
From the php manual:
<?php
function nicetime($date)
{
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago
?>
http://www.php.net/manual/de/function.time.php#89415
Print the timestamp in the regular way using PHP and use the timeago plugin for jQuery to do the fancy stuff.
You can use something like this
$time = $now - $time;
if ($time < 60) {
$name = "second";
}
elseif ($time < 3600)
{
$time /= 60;
$name = "minute";
}
elseif ($time < 86400)
{
$time /= 3600; //86400;
$name = "hour";
}
elseif ($time < 604800)
{
$time /= 86400; //604800;
$name = "day";
}
elseif ($time < 31536000)
{
$time /= 604800; //31536000;
$name = "week";
}
else
{
$time /= 31536000;
$name = "year";
}
Related
This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 6 years ago.
I am new in PHP. I am getting time difference in below time stamp code.
public function time_ago($date) {
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
I am getting result 5 hours even I post now. I want result like just now or 45 secs ago or 1 min ago.
Can anyone help me to resolve this issue.
You have to set timezone of your location.
public function time_ago($date) {
date_default_timezone_set(date_default_timezone_get());
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
I honestly have no idea on how to explain this, so if you look at http://www.gw2lfg.com, the elapsed time column is what I am trying to do, determine if something is a minute old, two minutes old, etc. thanks for all the help
your looking for time elapse you can do the following.
UNIX_TIMESTAMP('2012-12-01 12:12:12') - UNIX_TIMESTAMP(CURTIME());
you can then convert it into seconds and minutes.
Try this working example,
function time_ago($date)
{
if (empty($date)) { // $date=> mysql timestatmp
return "No date provided";
}
$periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if (empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if ($now >= $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if ($difference != 1 && $j != 0) {
$periods[$j].= "s";
}
if($difference!=0)
return "$difference $periods[$j] {$tense}";
else
return "a few seconds ago";
}
Update:
Usage:
echo time_ago('2013-07-01 11:30:00'); // Output: 5 mins ago
I have this function to work out the relative time that has elapsed since a certain date,
function nicetime($date) {
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
I am passing the function a date as "2011-01-16 12:30", however I am getting bad date returned which means that $unix_date is empty however if I die in the function I get the $date that is passed to the function however it gets return with a ( before it, below is how I am calling the method.
echo nicetime(date('Y:m:d G:i', $a['created_at']))
echo nicetime("2011-01-16 12:30");
works fine for me. I would suggest you ensure
$a['created_at']
is giving you what you thing it is. Maybe try
$dtDate = $a['created_at'];
$strDate = date('Y:m:d G:i', $dtDate );
echo $strDate;
echo nicetime($strDate);
Make sure $strDate is what you think it is.
Does anyone if php has built in functionally to do this?
Thanks
There is no built in function for this....but the following function will do it.
<?php
function nicetime($date)
{
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Bad date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "from now";
}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago
?>
$date = "2009-03-04 17:45";
$result = nicetime($date);
will only return 2 Days Ago
But if u want to get results like 1 sec ago, 10 secs ago, u also need to add seconds in $date
$date = "2009-03-04 17:45:20";
$result = nicetime($date);
So when u add a fresh db entry and u refresh the page immediately, u wil get the result as 1 sec ago
here is my php code.....
<?php
function nicetime($date)
{
if(empty($date)) {
return "No date provided";
}
$periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$unix_date = strtotime($date);
// check validity of date
if(empty($unix_date)) {
return "Incorrect Date";
}
// is it future date or past date
if($now > $unix_date) {
$difference = $now - $unix_date;
$tense = "ago";
} else {
$difference = $unix_date - $now;
$tense = "ago";
}
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
if($difference != 1) {
$periods[$j].= "s";
}
return "$difference $periods[$j] {$tense}";
}
?>
but it is not showing the time correctly....and if any current time is given to it is showing 25-35 seconds ago and so on....but i want to make a time difference funtion like digg.com in a format like 2 Mints AND 3 Sec ago... how can i do that
Try it to do with the other way - at the beginning multiply all $lengths values (i.e. $multiplied).
1. Divide $difference with that value.
2. If >= 1 then you have a solution.
Else divide $multiplied with the array_pop($lengths) value. Go to 2.
3. You have the solution.
I think it would be it.
BTW - $tense is always "ago".
Have a look at and learn from the Date::span and Date::fuzzy_span methods of the Kohana Date class.