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.
Related
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');
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.
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?
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";
}
}
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";
}
}
?>