PHP How to find the time elapsed since a date time?
Most of the answers seem focused around converting the date from a string to time. It seems you're mostly thinking about getting the date into the '5 days ago' format, etc.. right?
This is how I'd go about doing that:
$time = strtotime('2010-04-28 17:25:43');
echo 'event happened '.humanTiming($time).' ago';
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$time = ($time<1)? 1 : $time;
$tokens = array (
31536000 => 'year',
2592000 => 'month',
604800 => 'week',
86400 => 'day',
3600 => 'hour',
60 => 'minute',
1 => 'second'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
}
}
I haven't tested that, but it should work.
The result would look like
event happened 4 days ago
or
event happened 1 minute ago
cheers
How to find the time elapsed since a date time and what time is it like notification facebook?
I have updated your function. Please add required validation
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime();
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k ;//. ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
if(!empty($string['y']) || !empty($string['m']) || !empty($string['w']) )
{
echo showdate($datetime,'w')."<br>";
}
elseif(!empty($string['d']) )
{
if($string['d'] > 1)
{
echo showdate($datetime,'w')."<br>";
}
else
{
echo showdate($datetime,'d')."<br>";
}
}
elseif(!empty($string['h']) )
{
echo implode(', ', $string) . 'h ago';//"<br>";
}
elseif(!empty($string['i']) )
{
echo implode(', ', $string) . 'mints ago';//"<br>";
}
elseif( !empty($string['s']))
{
echo 'just now';//"<br>";
}
// return $string ? implode(', ', $string) . ' ago' : 'just now';
}
function showdate($dt,$type)
{
$mydt = new DateTime($dt);
if($type == 'd')
{
$str = 'Yesterday at '.$mydt->format('H:i:s ');
}
elseif($type == 'h')
{
//$str = 'Yesterday at '.$mydt->format('H:i:s ');
}
else
{
$str = $mydt->format('M d');
$str .= ' at '.$mydt->format('H:i:s ');
}
return $str;
}
$created_date = '2016-06-29 04:50:00';
echo time_elapsed_string($created_date);
PHP find the time elapsed since a date time inside a foreach loop
Thanks to @Clément Malet to point me in the right direction, DateTime::diff helped me to get the solution:
<?php foreach($comments as $comment): ?>
<?php
// foreach items
$data = $comment['cacheDate']; // this store a unix value like 1404992204999
// foreach items
<!-- html code -->
<div class="message">
<p class="data"><strong>Published: </strong>
<?php
$date1 = $data;
$date2 = time();
$subTime = $date1 - $date2;
$y = ($subTime/(60*60*24*365));
$d = ($subTime/(60*60*24))%365;
$h = ($subTime/(60*60))%24;
$m = ($subTime/60)%60;
echo $h." hour and " . $m." minutes"; // no need for years and days
?>
<strong>ago </strong>
</div>
<?php endforeach; ?>
<?php else: ?>
<h2>There are no comments yet</h2>
<?php endif; ?>
And the final output is like: Published: 10 hour and 25 minutes ago
Check if time elapsed since date is more than X days
You can use DateTime
class for this. Example:
$dt = "2011-03-19 10:05:44";
$date = new DateTime($dt);
$now = new DateTime();
$diff = $now->diff($date);
if($diff->days > 90) {
echo 'its greater than 90 days';
}
How do I extract the number of seconds elapsed from a specific date?
See my answer here:
Calculate number of hours between 2 dates in PHP
Date1 would be the date, when the question was posted and Date2 would be the current date. This version of comparing respects leap-seconds and leap-years and timezones, so it's more detailed than just comparing two unix-timestamps.
How to get time difference in minutes in PHP
Subtract the past most one from the future most one and divide by 60.
Times are done in Unix format so they're just a big number showing the number of seconds from January 1, 1970, 00:00:00 GMT
Related Topics
Open_Basedir Restriction in Effect. File(/) Is Not Within the Allowed Path(S):
Matching Unicode Letter Characters in Pcre/PHP
Multiple Returns from a Function
What Does the Variable $This Mean in PHP
Sort Array of Objects by One Property
How to Retrieve Images from MySQL Database and Display in an HTML Tag
Detect Encoding and Make Everything Utf-8
How to Convert an Image to Base64 Encoding
Parse Query String into an Array
File_Get_Contents(): Ssl Operation Failed With Code 1, Failed to Enable Crypto
How to Get Parameters from a Url String
PHP: How to Send Http Response Code
How to Close a Connection Early
How to Use MySQLi Prepared Statements
How to Make a Calculator in PHP