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.
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.
PHP Custom Time Ago Function
simple PHP Source code
$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
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 :
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.