Convert date / time in time with php

Hello in my database date / time in this format

2010-06-01T18:20:25+0000

I would like to repeat this time elapsed from this date / time

eg.

4 days 3 hours 36 minutes and 4 seconds

Is it possible?

+3
source share
5 answers

Below is the function I wrote for this. Feel free to use it.

/**
 * Returns rough (in largest single unit) time elapsed between two times.
 * @param int $iTime0  Initial time, as time_t.
 * @param int $iTime1  Final time, as time_t. 0=use current time.
 * @return string Time elapsed, like "5 minutes" or "3 days" or "1 month".
 *              You might print "ago" after this return if $iTime1 is now.
 * @author Dan Kamins - dos at axonchisel dot net
 */
function ax_getRoughTimeElapsedAsText($iTime0, $iTime1 = 0)
{
    if ($iTime1 == 0) { $iTime1 = time(); }
    $iTimeElapsed = $iTime1 - $iTime0;

    if ($iTimeElapsed < (60)) {
        $iNum = intval($iTimeElapsed); $sUnit = "second";
    } else if ($iTimeElapsed < (60*60)) {
        $iNum = intval($iTimeElapsed / 60); $sUnit = "minute";
    } else if ($iTimeElapsed < (24*60*60)) {
        $iNum = intval($iTimeElapsed / (60*60)); $sUnit = "hour";
    } else if ($iTimeElapsed < (30*24*60*60)) {
        $iNum = intval($iTimeElapsed / (24*60*60)); $sUnit = "day";
    } else if ($iTimeElapsed < (365*24*60*60)) {
        $iNum = intval($iTimeElapsed / (30*24*60*60)); $sUnit = "month";
    } else {
        $iNum = intval($iTimeElapsed / (365*24*60*60)); $sUnit = "year";
    }

    return $iNum . " " . $sUnit . (($iNum != 1) ? "s" : "");
}

To use this func, you first need to convert your time into time_t format (integer with a second since the "epoch"). Any of these PHP functions will probably help with this: http://php.net/strptime or http://php.net/strtotime .

+4
source

, .

    function ax_getRoughTimeElapsedAsText($iTime0, $iTime1 = 0)
    {
    if ($iTime1 == 0) { $iTime1 = time(); }
    $iTimeElapsed = $iTime1 - $iTime0;

    if ($iTimeElapsed < (60)) {
        return "Less than a minute ago";
    } else if ($iTimeElapsed < (60*60)) {
        $iNum = intval($iTimeElapsed / 60); $sUnit = "minute";
    } else if ($iTimeElapsed < (24*60*60)) {
        $iNum = intval($iTimeElapsed / (60*60)); $sUnit = "hour";
    } else if ($iTimeElapsed < (30*24*60*60)) {
        $iNum = intval($iTimeElapsed / (24*60*60)); $sUnit = "day";
    } else if ($iTimeElapsed < (365*24*60*60)) {
        $iNum = intval($iTimeElapsed / (30*24*60*60)); $sUnit = "month";
    } else {
        $iNum = intval($iTimeElapsed / (365*24*60*60)); $sUnit = "year";
    }

    return $iNum . " " . $sUnit . (($iNum != 1) ? "s" : "") . " ago";
    }
+2

timediff func :

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_timediff

pass the first parameter as the date, and the second parameter as of now ()

+1
source
function time_ago($timestamp, $granularity = 2) {
  $timestamp = time() - $timestamp;
  $units = array('1 year|%d years' => 31536000, 
                 '1 week|%d weeks' => 604800, 
                 '1 day|%d days' => 86400, 
                 '1 hour|%d hours' => 3600, 
                 '1 min|%d mins' => 60, 
                 '1 sec|%d secs' => 1
                );
  $output = '';
  foreach ($units as $key => $value) {
    $key = explode('|', $key);
    if ($timestamp >= $value) {
      $pluralized = floor($timestamp / $value) > 1 ? 
                    sprintf($key[1], floor($timestamp / $value)) : 
                    $key[0];
      $output .= ($output ? ' ' : '') . $pluralized;
      $timestamp %= $value;
      $granularity--;
    }
    if ($granularity == 0) {
      break;
    }
  }
  return $output ? $output : "Just now";
}

It should be close.

Edit: the following line has been added: $ timestamp = time () - $ timestamp;

+1
source

u can extract this fron db and use the strtotime function, which will evolve in the era. and then use the date command to print it in whatever format you want.

-1
source

Source: https://habr.com/ru/post/1748021/


All Articles