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