Click here to Skip to main content
15,867,911 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello! So I have this system setup where it fetches a row [created_at] in the 'videos' table and displays in "Time Ago". The problem is that its not displaying correctly which is making me very confused. The time in the database is correct and matches with the time on windows, but once converted using the function timeAgo, its showing almost as if it's delayed 9 hours. I will post my code as well as a screenshot of the time in the database. Thank you in advance for the help and I hope this inst a silly mistake.

PICTURE EXAMPLE:
Imgur: The magic of the Internet[^]

What I have tried:

PHP
/* * * * * * * * * * * *
*  Index.php
* * * * * * * * * * * * */

 $curenttime= $row['created_at']; //which displays 2020-08-14 00:32:06 for the 1/only post submited 
 
$time_ago = strtotime($curenttime);

    echo timeAgo($time_ago);


/* * * * * * * * * * * *
*  Returns Time Ago
* * * * * * * * * * * * */
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 / 2592000 );
$years 		= round($time_elapsed / 31536000 );
// 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";
	}
}
}
Posted
Updated 13-Aug-20 19:40pm

1 solution

I believe the clue is with it being always 9 hours difference. Couldn't it be because of the timezone setup on your system?

I checked and PHP time() Function[^] provides the current time as a Unix timestamp in GMT. (timezone independent)

I believe that would be causing it.

Quote:
The time() function returns the current time in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).


I believe your time saved in database doesn't follow the same format. Please check and adjust your logic accordingly. Either bring both to same format (recommended) or adjust for the difference.

Refer: PHP: time - Manual[^]

To change timezone, example:
PHP
<?php
date_default_timezone_set("UTC");
echo "UTC:".time();
echo "<br>";

date_default_timezone_set("Europe/Helsinki");
echo "Europe/Helsinki:".time();
echo "<br>";
?>
 
Share this answer
 
v3
Comments
nosajcchio. 14-Aug-20 11:46am    
Thank you for your answer! I have done what you've requested but to little success. I went into Apache php.ini settings and changed 'date.timezone =America/New_York' as well as adding 'date_default_timezone_set("America/New_York");' but all they did was reduce the time from "9 hours ago" to "3 hours ago" on update time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900