Time ago function php - php

I'm trying to do a time ago function only going back 24 hours. So the values returned will be random like;
7 mins ago;
2 hours 39 mins ago;
5 hours ago;
I'm trying to amend this function but don't know how to limit how to limit only going back 24 hours.
function timeAgo($time_ago){
$cur_time = time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
// Seconds
if($seconds <= 60){
echo "$seconds seconds ago";
}
//Minutes
else if($minutes <=60){
if($minutes==1){
echo "one minute ago";
}
else{
echo "$minutes minutes ago";
}
}
//Hours
else if($hours <=24){
if($hours==1){
echo "an hour ago";
}else{
echo "$hours hours ago";
}
}
}
How do i solve this?

You do not need such complex things.
This is enough:
$hours = rand(0, 3);
$mins = rand(0, 59);
$secs = rand(0, 59);
$text = '';
if ($hours) $text = $hours . ' hours';
if ($mins) $text .= $mins . ' mins';
if ($secs) $text .= $secs . ' secs';
// The following is almost impossible to happen, but anyway...
if (!$hours && !$mins && !$secs) $text = rand(1, 59) . ' mins';
$text .= 'ago';

After the line $hours = round($time_elapsed / 3600);, add if($hours > 24) return;, then you can limit the hours fewer than 24.

Use DateTime.diff and use the properties for DateInterval
function timeAgo(DateTime $time_ago){
$cur_time = new DateTime();
$time_elapsed = $cur_time.diff($time_ago);
$seconds = $time_elapsed.s;
$minutes = $time_elapsed.i;
$hours = $time_elapsed.h;
$days = $time_elapsed.d;
//days
if($days > 1){
if($days==1){
echo "one day ago";
}else{
echo "$days days ago";
}
}
//Hours
else if($hours > 1){
if($hours==1){
echo "an hour ago";
}else{
echo "$hours hours ago";
}
}
//Minutes
else if($minutes > 0){
if($minutes==1){
echo "one minute ago";
}
else{
echo "$minutes minutes ago";
}
}
// Seconds
else if($seconds> 0){
echo "$seconds seconds ago";
}
}

Related

Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago…

In this already existing Q a found this :
$time_elapsed = timeAgo($time_ago); //The argument $time_ago is in timestamp (Y-m-d H:i:s)format.
//Function definition
function timeAgo($time_ago)
{
$time_ago = strtotime($time_ago);
$cur_time = time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
$days = round($time_elapsed / 86400 );
$weeks = round($time_elapsed / 604800);
$months = round($time_elapsed / 2600640 );
$years = round($time_elapsed / 31207680 );
// Seconds
if($seconds <= 60){
return "just now";
}
//Minutes
else if($minutes <=60){
if($minutes==1){
return "one minute ago";
}
else{
return "$minutes minutes ago";
}
}
//Hours
else if($hours <=24){
if($hours==1){
return "an hour ago";
}else{
return "$hours hrs ago";
}
}
//Days
else if($days <= 7){
if($days==1){
return "yesterday";
}else{
return "$days days ago";
}
}
//Weeks
else if($weeks <= 4.3){
if($weeks==1){
return "a week ago";
}else{
return "$weeks weeks ago";
}
}
//Months
else if($months <=12){
if($months==1){
return "a month ago";
}else{
return "$months months ago";
}
}
//Years
else{
if($years==1){
return "one year ago";
}else{
return "$years years ago";
}
}
}
But how i write to this my number in this formatting ? please help.
(Sorry for this but this filter dont allow me to post this big code so please ignore this info about nothing only for posting my question ok ignore this thing in brackets I am not engish so sorry. will it work now ?)
You give the function any string of time that strtotime() accepts: http://php.net/manual/en/function.strtotime.php
i.e. $time_ago_string = timeAgo('Jan 14th, 2007');

Convert timestamp like Facebook and Twitter

I am fetching time from server like this: 25-07-2015 12:25:28
Now I want to show it like this:
a few second ago
1 minute ago
30 minutes ago
1 Hour ago
12 Hours ago
and after 24 Hours ago
show me the date of that day like :
25 August 2015
the following code works. But no data validation done (eg: old>new)
<?php
$olddate = "25-08-2015 15:35:28"; //date as string
$now = time(); //pick present time from server
$old = strtotime( $olddate); //create integer value of old time
$diff = $now-$old; //calculate difference
$old = new DateTime($olddate);
$old = $old->format('Y M d'); //format date to "2015 Aug 2015" format
if ($diff /60 <1) //check the difference and do echo as required
{
echo intval($diff%60)."seconds ago";
}
else if (intval($diff/60) == 1)
{
echo " 1 minute ago";
}
else if ($diff / 60 < 60)
{
echo intval($diff/60)."minutes ago";
}
else if (intval($diff / 3600) == 1)
{
echo "1 hour ago";
}
else if ($diff / 3600 <24)
{
echo intval($diff/3600) . " hours ago";
}
else if ($diff/86400 < 30)
{
echo intval($diff/86400) . " days ago";
}
else
{
echo $old; ////format date to "2015 Aug 2015" format
}
?>
Change the looping if you can. Logic remains same.
<?php
function timeago($timestamp){
$time_ago = strtotime($timestamp);
$current_time = time();
$time_difference = $current_time - $time_ago;
$seconds = $time_difference;
$minutes = round($seconds / 60); // value 60 is seconds
$hours = round($seconds / 3600); //value 3600 is 60 minutes * 60 sec
$days = round($seconds / 86400); //86400 = 24 * 60 * 60;
$weeks = round($seconds / 604800); // 7*24*60*60;
$months = round($seconds / 2629440); //((365+365+365+365+366)/5/12)*24*60*60
$years = round($seconds / 31553280); //(365+365+365+365+366)/5 * 24 * 60 * 60
if ($seconds <= 60){
return "Just Now";
} else if ($minutes <= 60){
if ($minutes == 1){
return "one minute ago";
} else {
return "$minutes minutes ago";
}
} else if ($hours <= 24){
if ($hours == 1){
return "an hour ago";
} else {
return "$hours hrs ago";
}
} else if ($days <= 7){
if ($days == 1){
return "yesterday";
} else {
return "$days days ago";
}
} else if ($weeks <= 4.3){
if ($weeks == 1){
return "a week ago";
} else {
return "$weeks weeks ago";
}
} else if ($months <= 12){
if ($months == 1){
return "a month ago";
} else {
return "$months months ago";
}
} else {
if ($years == 1){
return "one year ago";
} else {
return "$years years ago";
}
}
}
?>
Have you looked at the Date function in php?

Timesatmp to years ago conversion php

Below is the code that converts timestamp into "X" years ago....
but if i enter current time stamp its not function properly..please help
This is the function that converts the timestamp
<?php
function timeAgo($time_ago){
$cur_time = time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
$days = round($time_elapsed / 86400 );
$weeks = round($time_elapsed / 604800);
$months = round($time_elapsed / 2600640 );
$years = round($time_elapsed / 31207680 );
// Seconds
if($seconds <= 60){
echo "$seconds seconds ago";
}
//Minutes
else if($minutes <=60){
if($minutes==1){
echo "one minute ago";
}
else{
echo "$minutes minutes ago";
}
}
//Hours
else if($hours <=24){
if($hours==1){
echo "an hour ago";
}else{
echo "$hours hours ago";
}
}
//Days
else if($days <= 7){
if($days==1){
echo "yesterday";
}else{
echo "$days days ago";
}
}
//Weeks
else if($weeks <= 4.3){
if($weeks==1){
echo "a week ago";
}else{
echo "$weeks weeks ago";
}
}
//Months
else if($months <=12){
if($months==1){
echo "a month ago";
}else{
echo "$months months ago";
}
}
//Years
else{
if($years==1){
echo "one year ago";
}else{
echo "$years years ago";
}
}
}
?>
This is where it is called from
<?php
$curenttime="2015-06-1 4:35";
$time_ago =strtotime($curenttime);
echo timeAgo($time_ago);
?>
Output
4weeks
Seems to me like it works.
$curenttime="2015-06-1 4:35";
That is the first June, today is the first July, so the date you entered was 4 weeks ago.

How can i calculate the time of ad posting using php and mysql

Hello Everyone,
Can somebody tell me how can i count a time of a post that is online. Means i have a site of adposting, i want to count how can i calculate the online time of ad is posted, like
2 Days gone ad is online
3 Days gone ad is online
Here is an example
http://www.buyandsell.ie/motors/classic-cars/kerry/head-gasket-sealer-3
You can see in this site, online time is 9 days since ad is posting.
Suppose you have posted an ad on Christmass. The date is saved in mysql as 2012-12-25. Now you want to display how many days its has been posted. Use DateTime, DateInterval classes like this.
$d = DateTime::createFromFormat("Y-m-d", "2012-12-25");
$interval = $d->diff(new DateTime());
echo $interval->format("%a days"); // echos '6 days'
See more the code in action
<?php
//time() will give current time and
//$time will have the time from database when the post was posted on your site.
$time_difference = time() - $time ;
//calculate the difference and show accordingly.
$seconds = $time_difference ;
$minutes = round($time_difference / 60 );
$hours = round($time_difference / 3600 );
$days = round($time_difference / 86400 );
$weeks = round($time_difference / 604800 );
$months = round($time_difference / 2419200 );
$years = round($time_difference / 29030400 );
if($seconds <= 60)
{
echo "<font id='big'>a few seconds ago</font>";
}
//Minutes
else if($minutes <=60)
{
if ($minutes==1) {
echo "1 minute ago";
}
else {
echo $minutes." minutes ago";
}
}
else if($hours <=24) {
if ($hours==1) {
echo "1 hour ago";
}
else {
echo $hours." hours ago";
}
}
else if($days <= 7)
{
if ($days==1) {
echo "Yesterday";
}
else {
echo $days." days ago";
}
}
else if($weeks <= 4)
{
if ($weeks==1) {
echo "1 week ago";
}
else {
echo $weeks." weeks ago";
}
}
else if($months <=12)
{
if ($months==1) {
echo "1 month ago";
}
else {
echo $months." months ago";
}
}
else
{
if ($years==1) {
echo "1 year ago";
}
else {
echo $years." years ago";
}
}
?>

Convert mysql datetime into simple text

I have a MySql datetime value like "2012-04-17 20:48:29". I want to convert this to a simple text like "10 days ago". I want to do this in either php or javascript! I tried to create my own algorithm to do this. But is there an already available solution for doing this?
you could use this pattern
$date = "2012-04-17 20:48:29";
$seconds = time() - strtotime($date);
$days = floor($seconds / 86400);
$seconds -= $days * 86400;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
echo "$days days, $hours hours, $minutes minutes, $seconds seconds ago";
you should of course add some conditions before echoing the result. to only show 1 minute ago, or 3 hours ago, or 10 days ago...
With this function you'll get outputs like:
1 minute
5 minutes
15 hours
4 days
2 months
1.5 years
function time_ago_in_words($time) {
$from_time = strtotime($time);
$to_time = strtotime(gmd());
$distance_in_minutes = round((($to_time - $from_time))/60);
if ($distance_in_minutes < 0)
return (string)$distance_in_minutes.'E';
if (between($distance_in_minutes, 0, 1))
return '1 minute';
elseif (between($distance_in_minutes, 2, 44))
return $distance_in_minutes.' minutes';
elseif (between($distance_in_minutes, 45, 89))
return '1 hour';
elseif (between($distance_in_minutes, 90, 1439))
return round($distance_in_minutes/60).' hours';
elseif (between($distance_in_minutes, 1440, 2879))
return '1 day';
elseif (between($distance_in_minutes, 2880, 43199))
return round($distance_in_minutes/1440).' days';
elseif (between($distance_in_minutes, 43200, 86399))
return '1 month';
elseif (between($distance_in_minutes, 86400, 525959))
return round($distance_in_minutes/43200).' months';
elseif ($distance_in_minutes > 525959)
return number_format(round(($distance_in_minutes/525960), 1), 1).' years';
}
So you could do:
// Last time you logged in: 15 days ago.
Last time you logged in: <?php echo time_ago_in_words($user['last_logged_in']) ?> ago.
// We haven't seen you for 15 days!
We haven't seen you for <?php echo time_ago_in_words($user['last_logged_in']) ?>!
With PHP, you can call strftime to get different out put. Look here for more details.
There is also a jQuery plugin that you might look at as well: jQuery-dateFormat
Have fun!
I use this function:
function duration($integer)
{
$seconds=$integer;
$minutes = 0;
$hours = 0;
$days = 0;
$weeks = 0;
$return = "";
if ($seconds/60 >=1)
{
$minutes=floor($seconds/60);
if ($minutes/60 >= 1)
{ # Hours
$hours=floor($minutes/60);
if ($hours/24 >= 1)
{ #days
$days=floor($hours/24);
if ($days/7 >=1)
{ #weeks
$weeks=floor($days/7);
if ($weeks>=2) $return="$weeks Weeks";
else $return="$weeks Week";
} #end of weeks
$days=$days-(floor($days/7))*7;
if ($weeks>=1 && $days >=1) $return="$return, ";
if ($days >=2) $return="$return $days days";
if ($days ==1) $return="$return $days day";
} #end of days
$hours=$hours-(floor($hours/24))*24;
if ($days>=1 && $hours >=1) $return="$return, ";
if ($hours >=2) $return="$return $hours hours";
if ($hours ==1) $return="$return $hours hour";
} #end of Hours
$minutes=$minutes-(floor($minutes/60))*60;
if ($hours>=1 && $minutes >=1) $return="$return, ";
if ($minutes >=2) $return="$return $minutes minutes";
if ($minutes ==1) $return="$return $minutes minute";
} #end of minutes
$seconds=$integer-(floor($integer/60))*60;
if ($minutes>=1 && $seconds >=1) $return="$return, ";
if ($seconds >=2) $return="$return $seconds seconds";
if ($seconds ==1) $return="$return $seconds second";
$return="$return.";
return $return;
}
echo duration(time() - strtotime($date));

Categories