1

How do I to get the minutes from a this datatime field

$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':'');
}

}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149

2 Answers2

0

With DateTime you can achieve it like this:

$d1 = new DateTime('2010-04-28 17:25:43'); // start time
$d2 = new DateTime(); // end time (now)

$diff_seconds = $d2->getTimestamp() - $d1->getTimestamp();
$diff_minutes = $diff_seconds/60; // get amount of minutes
echo round($diff_minutes);
pavlovich
  • 1,935
  • 15
  • 20
-1

Convert both to timestamps, subtract, and format again such as

$timestamp = strtotime('22-09-2008');
$since = time() - $timestamp;
echo date('Y-m-d H:i:s', strtotime($since));

Not tested, please check the date function documentation and this answer: How to calculate the difference between two dates using PHP?

Community
  • 1
  • 1
Jack
  • 436
  • 5
  • 13