TimeAgo function returning negative seconds - php

I have a function that returns how long ago the time was posted, but there seem be to a slight problem with it. The first time, $timeAgo1 works just fine, but the second time, $timeAgo2 seems to return negative seconds. How is that happening?
<?php
//*****************************************************START OF FUNCTION
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 );
echo $seconds."<br>";
if ($seconds <= 60) {
return "just now";
} //end of if ($seconds <= 60)
else if ($minutes <=60) {
if ($minutes == 1) {
return "one minute ago";
} //end of else if ($minutes <=60)
else {
return "$minutes minutes ago";
} //end of else not ($minutes == 1)
} //end of else if ($minutes <= 60)
else if ($hours <= 24) {
if ($hours == 1) {
return "an hour ago";
} //end of if ($hours == 1)
else {
return "$hours hours ago";
} //end of else not ($hours == 1)
} //end of else if ($hours <= 24)
else if ($days <= 7) {
if ($days == 1) {
return "yesterday";
} //end of else if ($days <= 7)
else {
return "$days days ago";
} //end of else not ($days == 1)
} //end of else if ($days <= 7)
else if ($weeks <= 4.3) {
if ($weeks == 1) {
return "a week ago";
} //end of if ($weeks == 1)
else {
return "$weeks weeks ago";
} //end of else not ($weeks == 1)
} //end of else if ($weeks <= 4.3)
else if ($months <= 12) {
if ($months == 1) {
return "a month ago";
} //end of if ($months == 1)
else {
return "$months months ago";
} //end of else not ($months == 1)
} //end of else if ($months <= 12)
else {
if ($years == 1) {
return "one year ago";
} //end of if ($years == 1)
else {
return "$years years ago";
} //end of else not ($years == 1)
} //end of last else
} //end of function timeAgo($time_ago)
//*****************************************************END OF FUNCTION
$date1 = "2016-02-10";
$time1 = "22:41:58";
$date2 = "2016-02-12";
$time2 = "15:25:57";
$timeAgo1 = timeAgo($date1.$time1);
$timeAgo2 = timeAgo($date2.$time2);
echo $timeAgo1."<br>".$timeAgo2;
?>

Set the timezone in the beginning of the script to the same date as original dates you are checking against, example:
date_default_timezone_set('America/Los_Angeles');
http://php.net/manual/en/function.date-default-timezone-set.php

It is probably something with your server because I used 2 different servers and the results are
166733
20094
2 days ago
6 hours ago
It is possible that your server is located somewhere else than you are, so the time(); function shows a different timestamp from what you might expect.
The timestamp of the second timeAgo is 1455290705 so your server is probably located somewhere where it is/was more than 1455290705.

Related

Hello I'm trying to print time elapsed since posted from a mysql timestamp

<?php
date_default_timezone_set('Africa/Harare');
function get_timeago($connect, $post_datetime){
$query = "SELECT * FROM posts WHERE post_datetime = :post_datetime";
$statement = $connect->prepare($query);
$statement->execute(
array(
':post_datetime' => $post_datetime
)
);
$timeago = $post_datetime;
$current_time = time();
$time_difference = $current_time - $timeago;
$seconds = $time_difference;
$minutes = round($seconds / 60);
$hours = round($seconds / 3600);
$days = round($seconds / 86400);
$weeks = round($seconds / 604800);
$months = round($seconds / 2629800); //((365 + 365 + 365 + 366)/4/12) * 24 * 3600
$years = round($seconds / 31557600); //$months * 12
if($seconds <= 60){
return 'Just Now';
} else if($minutes <= 60){
if($minutes == 1){
return 'a minute ago';
} else {
return '$minutes minutes ago';
}
} else if($hours <= 60){
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){
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 'a year ago';
} else {
return '$years years ago';
}
return $result;
}
<span class="description">Shared publicly <i class="fa fa-globe"></i> - '.get_timeago($connect, strtotime($row['post_datetime'])).'</span>
?>
it's outputting this instead:
Shared publicly - $minutes minutes ago
Your issue is this
'$years years ago'
Well part of it anyway. In PHP single quotes do not interpolate variables, they are not replaced and are instead treated as string literals.
Instead use the " double quote.
$foo = 'replaced';
echo "$foo"; //outputs replaced
echo '$foo'; //outputs $foo

Converting Seconds Into user friendly format using php

I have a large number in seconds that I'm looking to convert into a result for example;
But that value is stored in seconds 1728000.
This is my current code but nothing is returned. As you can see I'm trying to make it as Human-Like as possible.
function convert_seconds($seconds)
{
if ($seconds <= 60){
return "nill";
} else if ($seconds >= 60 || $seconds <= 3600){ # hr
$time = ($seconds) / 3600;
$time_val = "Hour";
} else if ($seconds >= 3600 || $seconds <= 86400){ # day
$time = 0;
$time = ($seconds) / 86400;
$time_val = "Day";
} else if ($seconds >= 86400 || $seconds <= 604800){ # week
$time = 0;
$time = ($seconds) / 604800;
$time_val = "Week";
}
if ($time_val == "Hour" || $time <= 120){
$time_val == "Hours";
} else if ($time_val == "Day" || $time <= 7200){
$time_val == "Days";
} else if ($time_val == "Week" || $time <= 172800){
$time_val == "Weeks";
}
return $time.' '.$time_val;
}
echo convert_seconds(1728000);
The code isn't presise enough, for example 60 seconds will return 0.0169444444444 Hours?? If anyone knows a better way I would love to hear.
You need to use a return statement. You also should use a single "=" to assign a variable.
function convert_seconds($seconds)
{
if ($seconds <= 60){
return "nill";
} else if ($seconds >= 60 || $seconds <= 3600){ # hr
$time = ($seconds) / 3600;
$time_val = "Hour";
} else if ($seconds >= 3600 || $seconds <= 86400){ # day
$time = 0;
$time = ($seconds) / 86400;
$time_val = "Day";
} else if ($seconds >= 86400 || $seconds <= 604800){ # week
$time = 0;
$time = ($seconds) / 604800;
$time_val = "Week";
}
if ($time_val == "Hour" || $time <= 120){
$time_val = "Hours";
} else if ($time_val == "Day" || $time <= 7200){
$time_val = "Days";
} else if ($time_val == "Week" || $time <= 172800){
$time_val = "Weeks";
}
return $time.' '.$time_val;
}
There are a few problems with your code. Firstly you need to use &&, not || in the conditions, otherwise your second condition is always true. Also, your conditions are such that $time will always be 0 or 1 since you are dividing by the biggest value it can be for that condition. Try this instead:
function convert_seconds($seconds) {
if ($seconds < 60){
return "nill\n";
}
else if ($seconds >= 60 && $seconds < 3600){ # hr
$time = floor($seconds / 60);
$time_val = "Minute";
}
else if ($seconds >= 3600 && $seconds < 86400){ # day
$time = floor($seconds / 3600);
$time_val = "Hour";
}
else if ($seconds >= 86400 && $seconds < 604800){ # week
$time = floor($seconds / 86400);
$time_val = "Day";
}
else if ($seconds >= 604800) {
$time = floor($seconds / 604800);
$time_val = "Week";
}
if ($time > 1) $time_val .= 's';
return "$time $time_val\n";
}
echo convert_seconds(40);
echo convert_seconds(530);
echo convert_seconds(35930);
echo convert_seconds(240000);
echo convert_seconds(2345775);
Output:
nill
8 Minutes
9 Hours
2 Days
3 Weeks
I've assumed you want integer output, if you want decimal (e.g. 8.25 Hours), you can change the floor to an appropriate round e.g. $time = round($seconds / 60, 2);
Demo on 3v4l.org

Time ago PHP with Current timestamp

I am French and this text comes from an online translator. I apologize in advance for misspellings.
Context: I'm trying to display in a page, the time difference between the creation of my article and the date of the day. Make a system similar to facebook, for comments posted.
Example "1 day ago".
Problem: When I apply my function on my article, it always shows me "just now" even several minutes later. I can dig my head but I do not understand it. Can you guide me to a track?
Here are the elements that can help you.
My method in my article class:
enter code here public function getDate_creation() {
return $this->date_creation;
}
public function articleTimeAgo()
{
$time_ago = strtotime($this->getDate_creation());
$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) //4.3 == 52/12
{
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";
}
}
}
My view :
<?php if (sizeof($articles) > 0) :
foreach ($articles as $article) : ?>
<div class="children">
<h3><?php echo (empty($article->getAuteur()) ? 'Anonyme' : $article->getAuteur()) ; ?></h3>
<p><?php echo $article->getDescription(); ?></p>
<span><p><?php echo $article->articleTimeAgo(); ?></p></span>
</div>
<?php endforeach;
endif; ?>
I have doubts about the format of my fields in database, so I give you the format of the fields "date_creation":
type = timestamp
value = CURRENT_TIMESTAMP.
I hope I have been clear enough. I remain at your disposal for any other information.

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?

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