Hello got problem can't figure out why my timeago function wont work keep display 49 years? when its created at March 14, 2018 1:06pm don't know what went wrong still won't display correct time ago it should be 1 second or 6 second once i press submit but it would display instead 49years.
enter image description here
Here is my ticket.php
public static function getTicket(){
$sql = "SELECT * FROM `tickets` ";
$result = $mysqli->query($sql);
echo '<table class="table table-hover table-striped table-responsive"><th>User</th><th>Message</th><th>Created</th><th>Last update</th>';
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr class="table_row">
<td>' . $row['Message'] . '</td>
<td>' . date("F j, Y, g:i a", strtotime($row['date'])) . '</td>
<td>' . timeAgo($row['editdate']) . '</td>
<td><form method="POST">
<input name="ticketId" value="' . $row['ticketid'] . '" type="text" hidden />
<button type="submit" class="'.$class .'">'.$row['status'] .'</button></form>
</td>
</tr>';
}
Here is my timeAgo function.php
<?php
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 / 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";
}
}
}
?>
Please pass $row['editdate'] value converting in seconds using strtotime() function.
timeAgo(strtotime($row['editdate']));
After this,you can get a difference from current time and given time in seconds.Without converting in seconds,php considering default timestamp value as January 1 1970 00:00:00 GMT in your $time_ago variable and you got 49 year difference.
Hope you understand.
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 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?
Below is the code that converts timestamp into "X" years ago....
but if i enter current time stamp its not function properly..please help
This is the function that converts the timestamp
<?php
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 / 2600640 );
$years = round($time_elapsed / 31207680 );
// 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";
}
}
}
?>
This is where it is called from
<?php
$curenttime="2015-06-1 4:35";
$time_ago =strtotime($curenttime);
echo timeAgo($time_ago);
?>
Output
4weeks
Seems to me like it works.
$curenttime="2015-06-1 4:35";
That is the first June, today is the first July, so the date you entered was 4 weeks ago.
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";
}
}
?>