I've been struggling with this issue for a few days now, so I'm looking for any insights you may have. I have been using the following to input a timestamp on user posts:
//Timeframe
$date_time_now = date("Y-m-d H:i:s");
$start_date = new DateTime($date_time); //Time of post
$end_date = new DateTime($date_time_now); //Current time
$interval = $start_date->diff($end_date); //Difference between dates
if($interval->y >= 1) {
if($interval == 1)
$time_message = $interval->y . " year ago"; //1 year ago
else
$time_message = $interval->y . " years ago"; //1+ year ago
}
else if ($interval-> m >= 1) {
if($interval->d == 0) {
$days = " ago";
}
else if($interval->d == 1) {
$days = $interval->d . " day ago";
}
else {
$days = $interval->d . " days ago";
}
if($interval->m == 1) {
$time_message = $interval->m . " month " . $days;
}
else {
$time_message = $interval->m . " months " . $days;
}
}
else if($interval->d >= 1) {
if($interval->d == 1) {
$time_message = "Yesterday";
}
else {
$time_message = $interval->d . " days ago";
}
}
else if($interval->h >= 1) {
if($interval->h == 1) {
$time_message = $interval->h . " hour ago";
}
else {
$time_message = $interval->h . " hours ago";
}
}
else if($interval->i >= 1) {
if($interval->i == 1) {
$time_message = $interval->i . " minute ago";
}
else {
$time_message = $interval->i . " minutes ago";
}
}
else {
if($interval->s < 30) {
$time_message = "Just now";
}
else {
$time_message = $interval->s . " seconds ago";
}
}
In the html I have it outputting as:
<span class='comment-date'>$time_message</span>
Everything was working fine until recently, I didn't notice until a few days ago but I suspect it began when the year changed to 2019. What's happening is on some of the posts I'm getting the following error:
Notice: Object of class DateInterval could not be converted to int in C:\xampp\htdocs\LEARN_123\includes\classes\Post.php on line 311
Line 311 refers to if($interval == 1) in the above code. I'm also noticing that for posts that are over 1 year old, it's not deferring to the else statement $time_message = $interval->y . " years ago"; //1+ year ago. For some it just says 1 year ago, others I'm getting the Notice:
I've been going through this but can't seem to figure it out. Can anyone see what might be happening, or have any leads on how I can correct this?
if($interval == 1) should be if($interval->y == 1)
Also every where you ask if($interval->y|m|d|h|i|s==1
you don't need the $time_message = $interval->y|m|d|h|i|s . " frame ago";
it could more simply be $time_message = "1 frame ago";
where frame = year, month, day, hour, minute, second
or you could simplify more and get rid of the extra if/else statements with something like the following:
$time_message = $interval->y|m|d|h|i|s . " frame" . ($interval->y|m|d|h|i|s > 1 ? "s " : " ") . ago";
Just to close out the question & keep things tidy, I thought I would post the answer here. As pointed out in the comments, I'm receiving the Notice: on these b/c my conditional if($interval == 1) is not able to be converted since variable $interval is defined as a DateInterval object. The syntax was corrected to if($interval->y == 1) & this now functions as expected.
I'm trying to appreciate the avalanche that the small things can create, but sometimes it's just a pain in the a$$ too.
Related
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 know this question has been asked several times and I found so many tutorials, blog posts about converting timestamp to ago time in php..
I have tried countless codes and nothing seems to work for me...
I either get a blank page with no errors (i have error rerposting on my php page), or I get some strange numbers in my page..
so I thought someone here could shed a light on this for me..
Basically I am saving date like so:
$date = date('Y-m-d H:i:s');
I simply save it in mysql database...
and I echo it like so:
echo $date;
so now what I need to know is how I can convert the echo $date; to something like 1 minutes ago, 10 minutes ago, 1 hour ago etc etc every time the page closes and reopens?
I did try so many functions that I found on google and noon seem to do anything!
could someone please advise on this issue?
Thanks
EDIT:
I used this code as stated in the answer but I still get the $date echo-ed exactly the same way as its stored in the database which is this format: 2014-10-06 22:54:54
$date = date('Y-m-d H:i:s');
$time1 = new DateTime($date);
$now = new DateTime();
$interval = $time1->diff($now);
if ($interval->y) $date = $interval->y . ' years';
elseif ($interval->m) $date = $interval->m . ' months';
elseif ($interval->d) $date = $interval->d . ' days';
elseif ($interval->h) $date = $interval->h . ' hours';
elseif ($interval->i) $date = $interval->i . ' minutes';
echo $date;
You should use the DateTime class to get the difference between 2 times, ie;
$time1 = new DateTime('2014-10-06 09:00:59');
$now = new DateTime();
$interval = $time1->diff($now,true);
and then use that difference (which is a DateInterval object, $interval) to find the smallest time difference like this;
if ($interval->y) echo $interval->y . ' years';
elseif ($interval->m) echo $interval->m . ' months';
elseif ($interval->d) echo $interval->d . ' days';
elseif ($interval->h) echo $interval->h . ' hours';
elseif ($interval->i) echo $interval->i . ' minutes';
else echo "less than 1 minute";
which should echo (at time of writing) 13 hours.
Hope this helps.
Check this function intval() - http://php.net/manual/en/function.intval.php
The following code should help you out
$seconds_ago = (time() - strtotime('2014-01-06 15:25:08'));
if ($seconds_ago >= 31536000) {
echo "Seen " . intval($seconds_ago / 31536000) . " years ago";
} elseif ($seconds_ago >= 2419200) {
echo "Seen " . intval($seconds_ago / 2419200) . " months ago";
} elseif ($seconds_ago >= 86400) {
echo "Seen " . intval($seconds_ago / 86400) . " days ago";
} elseif ($seconds_ago >= 3600) {
echo "Seen " . intval($seconds_ago / 3600) . " hours ago";
} elseif ($seconds_ago >= 60) {
echo "Seen " . intval($seconds_ago / 60) . " minutes ago";
} else {
echo "Seen less than a minute ago";
}
I have some strings like "3 days ago", "6 hours ago", "9 minutes ago", and "12 seconds ago". I'm new to regular expressions, so I'm not sure how to go about matching the "s" before " ago" so I can strip it out if needed.
Edit: My code in case it can help someone else...
function timeAgo ($timestamp) {
$difference = time() - strtotime($timestamp);
if ($difference > (60*60*24)) {
$difference = round($difference/60/60/24) . " days ago";
}
else if ($difference > (60*60)) {
$difference = round($difference/60/60) . " hours ago";
}
else if ($difference > 60) {
$difference = round($difference/60) . " minutes ago";
}
else if ($difference > 0) {
$difference = $difference . " seconds ago";
}
$int = filter_var($difference, FILTER_SANITIZE_NUMBER_INT);
if ($int == 1) {
$difference = preg_replace('/.(?= ago)/', '', $difference);
}
return $difference;
}
Try the below regex to match s which was just before to <space>ago,
.(?= ago)
DEMO
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 used many functions for relative time in php but that give different results...help me
My functions :
<?php
function pretty_relative_time($time) {
if ($time !== intval($time)) { $time = strtotime($time); }
$d = time() - $time;
if ($time < strtotime(date('Y-m-d 00:00:00')) - 60*60*24*3) {
$format = 'F j';
if (date('Y') !== date('Y', $time)) {
$format .= ", Y";
}
return date($format, $time);
}
if ($d >= 60*60*24) {
$day = 'Yesterday';
if (date('l', time() - 60*60*24) !== date('l', $time)) { $day = date('l', $time); }
return $day . " at " . date('g:ia', $time);
}
if ($d >= 60*60*2) { return intval($d / (60*60)) . " hours ago"; }
if ($d >= 60*60) { return "about an hour ago"; }
if ($d >= 60*2) { return intval($d / 60) . " minutes ago"; }
if ($d >= 60) { return "about a minute ago"; }
if ($d >= 2) { return intval($d) . " seconds ago"; }
else {return "Just Now"; }
}
function plural($num) {
if ($num != 1)
return "s";
}
function getRelativeTime($date) {
$diff = time() - strtotime($date);
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return "on " . date("F j, Y", strtotime($date));
}
echo pretty_relative_time('2012-08-06 8:04:15') ;echo "<br/>";
echo getRelativeTime('2012-08-06 8:04:15');
?>
OutPut :
Just Now // for first function
-15747 seconds ago // for second function
any settings in db ?.....i have used DATETIME for date...
You haven't made clear what output you're expecting, but from your later comment it should be "1 hour ago"? In which case I suspect you are suffering from timezone problems - most likely the dreaded "Daylight Savings Time". Try echoing date('Y-m-d H:i:s') in PHP and seeing if it comes out with the value you expect.
The discrepancy could be in the timezone set in php.ini, or the system clock of the webserver itself, not matching the timezone setting of the database, or the system clock on that server.