PHP Time Ago Helper Function

Today, We want to share with you PHP Time Ago Helper Function.In this post we will show you How to display time in x days ago in php?, hear for php check if timestamp is older than 24 hours we will give you demo and example for implement.In this post, we will learn about convert timestamp to time ago php with an example.

PHP Time Ago Helper Function

There are the Following The simple About Time Ago PHP Function to Display Elapsed Time Full Information With Example and source code.

As I will cover this Post with live Working example to develop Converting this date into time ago, so the Converting timestamp to time ago in PHP is used for this example is following below.

Read Also:  jQuery Dynamically addClass and removeClass

PHP program to convert timestamp to time ago

HTML Date Input

index.php

This HTML will show a form with a date field to get a date from the user.

<form method="post" name="liveTimeStepmAgo">Enter Your String Based Date:
<input name="date-user-inp" type="date" value="<?php if(!empty($_POST[" />"/>
<input name="submit-date" type="submit" value="Submit Date" /></form><!--?php if(!empty($inputTimestemp)) { echo "Result: " . $inputTimestemp; } ?-->

PHP Custom Time Ago Function

simple PHP Source code

<!--?php $inputTimestemp = ""; if(!empty($_POST["date-user-inp"])) { $inputTimestemp = timeago($_POST["date-user-inp"]); } function timeago($date) { $timestamp = strtotime($date); $strTime = array("second", "minute", "hour", "day", "month", "year"); $milestone = array("60","60","24","30","12","10"); $currentTime = time(); if($currentTime >= $timestamp) {<br ?--> $convert_differnce = time()- $timestamp;
for($i = 0; $convert_differnce >= $milestone[$i] && $i < count($milestone)-1; $i++) { $convert_differnce = $convert_differnce / $milestone[$i]; } $convert_differnce = round($convert_differnce); return $convert_differnce . " " . $strTime[$i] . "(s) ago "; } } ?>

How to convert timestamp to time ago in PHP ?

index.php

<!--?php <br ?--> function convert_time_Ago($time) {
$convert_differnce = time() - $time;
$second = $convert_differnce;
$minutes = round($convert_differnce / 60 );
$hour = round($convert_differnce / 3600);
$days = round($convert_differnce / 86400 );
$weeks = round($convert_differnce / 604800);
$mnths = round($convert_differnce / 2600640 );
$yrs = round($convert_differnce / 31207680 );

if($second <= 60) {
echo "$second seconds ago";
}

else if($minutes <= 60) {
if($minutes==1) {
echo "one minute ago";
}
else {
echo "$minutes minutes ago";
}
}

else if($hour <= 24) {
if($hour == 1) {
echo "an hour ago";
}
else {
echo "$hour hours ago";
}
}

else if($days <= 7) {
if($days == 1) {
echo "Yesterday";
}
else {
echo "$days days ago";
}
}

else if($weeks <= 4.3) {
if($weeks == 1) {
echo "a week ago";
}
else {
echo "$weeks weeks ago";
}
}

else if($mnths <= 12) { if($mnths == 1) { echo "a month ago"; } else { echo "$mnths months ago"; } } else { if($yrs == 1) { echo "one year ago"; } else { echo "$yrs years ago"; } } } //Example 1 $curr_time = "2018-07-10 09:09:09"; $time_ago = strtotime($curr_time); echo convert_time_Ago($time_ago) . "\n"; //Example 2 $curr_time="2022-01-05 09:09:09"; $time_ago =strtotime($curr_time); echo convert_time_Ago($time_ago); ?>

Web Programming Tutorials Example with Demo

Read :

Read Also:  nested select sql

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about convert timestamp to time ago php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Wordpress $wpdb Insert Multiple Records
  2. Read Also:  Laravel 6 display flash message on view
  3. Read Also:  Simple php rating system with database Example
  4. Read Also:  Ajax Autocomplete Live Search Suggestion using PHP MySQL
  5. Read Also:  GO Program to Display Fibonacci Sequence
  6. Read Also:  How to Merge Two Eloquent Collections in Laravel?
  7. Read Also:  WooCommerce Get Product Prices
  8. Read Also:  How to (Easily) Change Date Format in codeigniter?
  9. Read Also:  Laravel Autocomplete Live Search from database
  10. Read Also:  Time Ago Function Helper Class in PHP
CLOSEX