Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to show last seen in user's profile in my php page. I am storing user's logout time in database as 2014-01-06 15:25:08 (store in $last_log) with DATETIME datatype. Now i want to display last seen x mins ago. And it's auto update in x day ago, x month ago.
I want same as here when we add comment & its time ".......ago" updates.
How can i display this.
// intval() - http://php.net/manual/en/function.intval.php
$seconds_ago = (time() - strtotime('2014-01-06 15:25:08'));
if ($seconds_ago >= 31536000) {
echo "Seen " . intval($seconds_ago / 31536000) . " years ago";
} elseif ($seconds_ago >= 2419200) {
echo "Seen " . intval($seconds_ago / 2419200) . " months ago";
} elseif ($seconds_ago >= 86400) {
echo "Seen " . intval($seconds_ago / 86400) . " days ago";
} elseif ($seconds_ago >= 3600) {
echo "Seen " . intval($seconds_ago / 3600) . " hours ago";
} elseif ($seconds_ago >= 60) {
echo "Seen " . intval($seconds_ago / 60) . " minutes ago";
} else {
echo "Seen less than a minute ago";
}
Try this function
function get_timeago( $ptime )
{
$etime = time() - $ptime;
if( $etime < 1 )
{
return 'less than '.$etime.' second ago';
}
$a = array( 12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach( $a as $secs => $str )
{
$d = $etime / $secs;
if( $d >= 1 )
{
$r = round( $d );
return 'about ' . $r . ' ' . $str . ( $r > 1 ? 's' : '' ) . ' ago';
}
}
}
Usage :
$timestamp = strtotime("2014-11-14 17:15:59");
echo get_timeago( $timestamp );
try something like this:
$datetime1 = new DateTime('2014-01-06 15:25:08');
$datetime2 = new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->format('%a days')."<br>";
echo $interval->m." Months";
for more read this:http://php.net/manual/en/datetime.diff.php
try this
$date1 = strtotime('2014-12-06 15:25:08');
$date2 = strtotime(date('Y-m-d H:i:s'));
$seconds_diff = $date2 - $date1;
echo round(abs($seconds_diff) / 60,2). " mins ago";
Use the date_diff function.
See http://php.net/manual/en/function.date-diff.php
"Powerful Function to get two date difference."
In short:
// $datetime1 and $datetime2 are UNIX timestamps.
$interval = date_diff($datetime1, $datetime2);
echo $interval->format($differenceFormat);
Related
Good day guys, I am having a bit trouble of how can I make this like xx month/s ago and year/s ago if the days reach the number of months/years. Is it possible ? Anyway I'm making a messaging module. This is my code. Advance thank you for your help guys
<?php
date_default_timezone_set('Asia/Manila');
$now = strtotime(date("Y-m-d H:i:s"));
$date = strtotime($key->message_date); //this is the date where message was sent
$dateDiff = abs($now - $date);
$fullDays = floor($dateDiff/(60*60*24));
if($fullDays==0)
{
echo " Today ";
}
else if($fullDays==1)
{
echo " Yesterday ";
}
else
{
echo $fullDays ." days ago";
}
$at=date('g:iA',$date)
?> at <?php echo $at?>
You can use this function you have Pass message time as an argument
function convert_time($ptime){
$etime = time() - $ptime;
if($etime < 1){
return '0 seconds';
}
$a = array( 12 * 30 * 24 * 60 * 60 => 'year',
30 * 24 * 60 * 60 => 'month',
24 * 60 * 60 => 'day',
60 * 60 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($a as $secs => $str){
$d = $etime / $secs;
if($d >= 1){
$r = round($d);
return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago';
}
}
}
In this function you can get "(1) second/minute/hour/day/month/year ago"
This question already has answers here:
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 8 years ago.
I am using the following php function to convert my standard DATETIME result into a facebook style time ago, which shows the number of seconds, minutes and hours, days etc of that date.
Here's my php function:
<?php
function pretty_relative_time($time) {
if ($time !== intval($time)) { $time = strtotime($time); }
$d = time() - $time;
if ($time < strtotime(date('Y-m-d 00:00:00')) - 60*60*24*3) {
$format = 'F j';
if (date('Y') !== date('Y', $time)) {
$format .= ", Y";
}
return date($format, $time);
}
if ($d >= 60*60*24) {
$day = 'Yesterday';
if (date('l', time() - 60*60*24) !== date('l', $time)) { $day = date('l', $time); }
return $day . " at " . date('g:ia', $time);
}
if ($d >= 60*60*2) { return intval($d / (60*60)) . " hours ago"; }
if ($d >= 60*60) { return "about an hour ago"; }
if ($d >= 60*2) { return intval($d / 60) . " minutes ago"; }
if ($d >= 60) { return "about a minute ago"; }
if ($d >= 2) { return intval($d) . " seconds ago"; }
return "a few seconds ago";
}
?>
And here's where I call the function:
echo pretty_relative_time($row1['date']);
the problem I'm having is the script almost works 100%, however if something is dated within an hour it only ever shows 'about an hour ago' even if I've posted something within a couple of minutes or seconds. Can someone please show me what I am doing wrong? My date is stored as a DATETIME stamp
You can use this function its working 100% correct. . .
function Myfunction ($time)
{
$time = time() - $time; // to get the time since that moment
$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':'');
}
}
And call in this way. .
$time = strtotime($time);
$time = Myfunction($time);
I know this question has been asked several times and I found so many tutorials, blog posts about converting timestamp to ago time in php..
I have tried countless codes and nothing seems to work for me...
I either get a blank page with no errors (i have error rerposting on my php page), or I get some strange numbers in my page..
so I thought someone here could shed a light on this for me..
Basically I am saving date like so:
$date = date('Y-m-d H:i:s');
I simply save it in mysql database...
and I echo it like so:
echo $date;
so now what I need to know is how I can convert the echo $date; to something like 1 minutes ago, 10 minutes ago, 1 hour ago etc etc every time the page closes and reopens?
I did try so many functions that I found on google and noon seem to do anything!
could someone please advise on this issue?
Thanks
EDIT:
I used this code as stated in the answer but I still get the $date echo-ed exactly the same way as its stored in the database which is this format: 2014-10-06 22:54:54
$date = date('Y-m-d H:i:s');
$time1 = new DateTime($date);
$now = new DateTime();
$interval = $time1->diff($now);
if ($interval->y) $date = $interval->y . ' years';
elseif ($interval->m) $date = $interval->m . ' months';
elseif ($interval->d) $date = $interval->d . ' days';
elseif ($interval->h) $date = $interval->h . ' hours';
elseif ($interval->i) $date = $interval->i . ' minutes';
echo $date;
You should use the DateTime class to get the difference between 2 times, ie;
$time1 = new DateTime('2014-10-06 09:00:59');
$now = new DateTime();
$interval = $time1->diff($now,true);
and then use that difference (which is a DateInterval object, $interval) to find the smallest time difference like this;
if ($interval->y) echo $interval->y . ' years';
elseif ($interval->m) echo $interval->m . ' months';
elseif ($interval->d) echo $interval->d . ' days';
elseif ($interval->h) echo $interval->h . ' hours';
elseif ($interval->i) echo $interval->i . ' minutes';
else echo "less than 1 minute";
which should echo (at time of writing) 13 hours.
Hope this helps.
Check this function intval() - http://php.net/manual/en/function.intval.php
The following code should help you out
$seconds_ago = (time() - strtotime('2014-01-06 15:25:08'));
if ($seconds_ago >= 31536000) {
echo "Seen " . intval($seconds_ago / 31536000) . " years ago";
} elseif ($seconds_ago >= 2419200) {
echo "Seen " . intval($seconds_ago / 2419200) . " months ago";
} elseif ($seconds_ago >= 86400) {
echo "Seen " . intval($seconds_ago / 86400) . " days ago";
} elseif ($seconds_ago >= 3600) {
echo "Seen " . intval($seconds_ago / 3600) . " hours ago";
} elseif ($seconds_ago >= 60) {
echo "Seen " . intval($seconds_ago / 60) . " minutes ago";
} else {
echo "Seen less than a minute ago";
}
I have a timestamp and would like to show my users... last sent 1 day, 23 hours, 54 minutes, and 33 seconds ago. I know how to get the difference in time...
$timePast = '2012-08-18 22:11:33';
$timeNow = date('Y-m-d H:i:s');
// gives total seconds difference
$timeDiff = strtotime($timeNow) - strtotime($timePast);
Now I am stuck not being able to show the time like above.
x day, x hours, x mins, x seconds where all the x's should add up to the total seconds time difference. I know the following...
$lastSent['h'] = round($timeDiff / 3600);
$lastSent['m'] = round($timeDiff / 60);
$lastSent['s'] = $timeDiff;
Need you help! Thanks in advance.
After this:
$timeDiff = strtotime($timeNow) - strtotime($timePast);
add:
if ($timeDiff > (60*60*24)) {$timeDiff = floor($timeDiff/60/60/24) . ' days ago';}
else if ($timeDiff > (60*60)) {$timeDiff = floor($timeDiff/60/60) . ' hours ago';}
else if ($timeDiff > 60) {$timeDiff = floor($timeDiff/60) . ' minutes ago';}
else if ($timeDiff > 0) {$timeDiff .= ' seconds ago';}
echo $timeDiff;
Don't do date math manually!
PHP can work out all of the date/time math for you, using the DateTime and DateInterval classes.
Getting an interval between two dates
$timePast = new DateTime('2012-08-18 22:11:33');
$timeNow = new DateTime;
$lastSent = $timePast->diff($timeNow);
// $lastSent is a DateInterval with properties for the years, months, etc.
Formatting example
A function for getting a formatted string might look like the following (though this is only one super-basic way, of many).
function format_interval(DateInterval $interval) {
$units = array('y' => 'years', 'm' => 'months', 'd' => 'days',
'h' => 'hours', 'i' => 'minutes', 's' => 'seconds');
$parts = array();
foreach ($units as $part => $label) {
if ($interval->$part > 0) {
$parts[] = $interval->$part . ' ' . $units[$part];
}
}
return implode(', ', $parts);
}
echo format_interval($lastSent); // e.g. 2 days, 24 minutes, 46 seconds
I took Kalpesh's code and made it work by using floor instead of round and by calculating the different frictions of the day. Here it goes:
function timeAgo ($oldTime, $newTime) {
$timeCalc = strtotime($newTime) - strtotime($oldTime);
$ans = "";
if ($timeCalc > 60*60*24) {
$days = floor($timeCalc/60/60/24);
$ans .= "$days days";
$timeCalc = $timeCalc - ($days * (60*60*24));
}
if ($timeCalc > 60*60) {
$hours = floor($timeCalc/60/60);
$ans .= ", $hours hours";
$timeCalc = $timeCalc - ($hours * (60*60));
}
if ($timeCalc > 60) {
$minutes = floor($timeCalc/60);
$ans .= ", $minutes minutes";
$timeCalc = $timeCalc - ($minutes * 60);
}
if ($timeCalc > 0) {
$ans .= "and $timeCalc seconds";
}
return $ans . " ago";
}
$timePast = '2012-08-18 22:11:33';
$timeNow = date('Y-m-d H:i:s');
$t = timeAgo($timePast, $timeNow);
echo $t;
Output:
1 days, 16 hours, 11 minutes and 18 seconds ago
You'll need a lot of if's, the modulus (%), floor() (not round())
Or Google ;-)
I want to convert a date string like "19.11.2009 14:00" into the age of it now like "for 2 minutes" or "for 1 week" or "for 2 days"
Is there some code around?
$dateString = strtotime('19.11.2009 14.00');
$now = time();
$time = $dateString - $now;
if($time > 60 && $time < 3600) echo $time/60.' minutes remaining';
else if($time > 3600 && $time < 86400) echo $time/3600.' hours remaining';
else if($time > 86400 && $time < 604800) echo $time/86400.' days remaining';
else if($time > 604800 && $time < 18144000) echo $time/604800.' weeks remaining';
else if($time > 18144000 && $time < 217728000) echo $time/18144000.' months remaining';
else if($time > 217728000) echo $time/217728000.' years remaining';
Something like this
define('MINUTE',60);
define('HOUR',60*MINUTE);
define('DAY',24*HOUR);
define('WEEK',7*DAY);
define('MONTH',30*DAY);
$pastDate=strtotime($dateString);
$seconds=time()-$pastDate;
if ($seconds>MONTH)
return $seconds/MONTH . " months";
if ($seconds>WEEK)
return $seconds/WEEK . " weeks";
if ($seconds>DAY)
return $seconds/DAY . " days";
if ($seconds>HOUR)
return $seconds/HOUR . " hours";
if ($seconds>MINUTE)
return $seconds/MINUTE . " minutes";
return $seconds . " seconds";
If you're in PHP 5.3 you could also use DateTime:diff.
$start = new DateTime('now');
$time_span = $start->diff(new DateTime($dateString));
var_dump($time_span);
This may not be 100% what you want obviously -- others have given you great answers -- but something like this may be a good alternative to a "human readable" date format.
I dug this up from some code I used quite a while ago. I haven't tested this in a while, but last I remember it worked great. I wanted to replicate something like what Facebook uses, like "5 seconds ago", but it also works for the future, using "in ..." instead of "... ago". You can probably modify this to get as much or as little detail as you want.
/**
* Returns the amount of time that has passed from the current date
* or the amount of time from the current date until the specified date
*
* Returns in the form of a partial sentence. Some examples:
*
* In 25 days
* Tomorrow
* Yesterday
* 4 months ago
* Next month
* Last month
* (etc)
*
* #param string $date
* #return string
*/
public static function calculateHowLong($date) {
// start by converting to unix time
$when = date("U", strtotime($date));
$isPast = ($when < time());
$how_long = abs(time() - $when);
if ($how_long < 60) {
$return = "{$how_long} seconds";
if ($isPast) $return .= " ago"; else $return = "In {$return}";
} elseif ($how_long < 60 * 60) {
$return = (int) ($how_long / 60) . " minutes";
if ($isPast) $return .= " ago"; else $return = "In {$return}";
} elseif ($how_long < 60 * 60 * 24) {
$return = (int) ($how_long / (60 * 60)) . " hours";
if ($isPast) $return .= " ago"; else $return = "In {$return}";
} elseif ($how_long < 60 * 60 * 24 * 2) {
if ($isPast) $return = "Yesterday"; else $return = "Tomorrow";
} elseif ($how_long < 60 * 60 * 24 * 7) {
$return = (int) ($how_long / (60 * 60 * 24)) . " days";
if ($isPast) $return .= " ago"; else $return = "In {$return}";
} elseif ($how_long < 60 * 60 * 24 * 13) {
if ($isPast) $return = "Last week"; else $return = "Next week";
} elseif ($how_long < 60 * 60 * 24 * 7 * 4) {
$return = (int) ($how_long / (60 * 60 * 24 * 7)) . " weeks";
if ($isPast) $return .= " ago"; else $return = "In {$return}";
} elseif ($how_long < 60 * 60 * 24 * 30 * 2) {
if ($isPast) $return = "Last month"; else $return = "Next month";
} elseif ($how_long < 60 * 60 * 24 * 30 * 12) {
$return = (int) ($how_long / (60 * 60 * 24 * 30)) . " months";
if ($isPast) $return .= " ago"; else $return = "In {$return}";
} else {
if ($isPast) $return = "More than 1 year ago"; else $return = "In more than 1 year";
}
return $return;
}
It's probably a little sloppy, but feel free to make it better.
I see earlier answers offering long-winded, "Write Everything Twice" (WET) instead of "Don't Repeat Yourself" (DRY) code. Worse, they are not leveraging the modern might of PHP's DateTime class. All of the obscurity and verbosity of performing mathematical calculations on unix time can be avoided. #Sergi hints in the right direction, but neglects to offer a complete solution.
Because the scope of this question states that incoming dates will be in the past and they are to be compared to the current date/time, I'll offer the following snippet to provide generalized English expressions as described in the question.
To cover cases where a number of weeks is desired, a condition needs to be implemented because the DateTime diff object does not provide a property for w (weeks). This is the only part of the snippet that requires a mathematical calculation.
Code: (Demo)
$date = '10.11.2021 14:00';
$diff = (new DateTime($date))->diff(new DateTime());
$lookup = [
'y' => 'year',
'm' => 'month',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
];
foreach ($lookup as $property => $word) {
if ($diff->$property) {
if ($property === 'd' && $diff->$property >= 7) {
$diff->w = (int)($diff->$property / 7);
$property = 'w';
$word = 'week';
}
$output = "for {$diff->$property} $word" . ($diff->$property !== 1 ? 's' : '');
break;
}
}
echo $output ?? 'for mere seconds';
Output (as table using 03.12.2021 13:00 as static current datetime):
input
output
03.12.2021 13:00
for mere seconds
03.12.2021 12:59
for 1 minute
03.12.2021 12:32
for 28 minutes
03.12.2021 11:21
for 1 hour
03.12.2021 09:12
for 3 hours
02.12.2021 02:03
for 1 day
27.11.2021 11:11
for 6 days
25.11.2021 13:11
for 1 week
04.11.2021 15:15
for 4 weeks
10.10.2021 17:59
for 1 month
27.04.2021 01:35
for 7 months
27.04.2020 01:35
for 1 year
27.04.2009 01:35
for 12 years