date to time ago ? without useing strtime?

ok i have a date

date('Y-m-d H:i:s');

how can we make it like 0 seconds ago or x year x month x day x minutes x seconds ago ? but without useing the str_time ( php 5.3.3 )

edit*

i mean is some.date - time.now = someyear some month someday someminutes someseconds ago. like facebook or stackoverflow ( updated. i mean like that. – Adam Ramadhan 2 mins ago edit )

edit* some say to use sktime ?

the 2 mins ago.

Thanks

Adam Ramadhan


You should try out this (looks exactly like the krike answer but this one is fixed!)

<?php
function ago($timestamp){
  $difference = time() - $timestamp;
  $periods = array("second", "minute", "hour", "day", "week", "month", "years", "decade");
  $lengths = array("60","60","24","7","4.35","12","10");
  for($j = 0; $difference >= $lengths[$j]; $j++)
  $difference /= $lengths[$j];
  $difference = round($difference);
  if($difference != 1) $periods[$j].= "s";
  $text = "$difference $periods[$j] ago";
  return $text;
 }
?>

Just do this---

echo ago($timeofthethingyouwant);

And remember this works with this type of format of time----

time(); //Or it looks like this 1299951275!

This works for sure and it is great and light weight script! For more head to this link--

http://drupal.org/node/61565

Hope it works!


Why not just subtract 30 from the output of time() ?

date('Y-m-d H:i:s',time()-30);

time() returns the current time measured in terms of number of seconds since epoch, so subtracting 30 from it gives the timestamp of the time 30 sec ago


function nicetime($fromDate, $toDate = NULL, $precision = -1, $separator = ', ', $divisors = NULL) {

    if ( is_null( $toDate ) ) {
        $toDate = $this->date_get('Asia/Jakarta');
    }

    // Determine the difference between the largest and smallest date
    $dates = array(strtotime($fromDate), strtotime($toDate));
    $difference = max($dates) - min($dates);

    // Return the formatted interval
    return $this->format_interval($difference, $precision, $separator, $divisors);

}

/**
* Formats any number of seconds into a readable string
*
* @param int Seconds to format
* @param string Seperator to use between divisors
* @param int Number of divisors to return, ie (3) gives '1 Year, 3 Days, 9 Hours' whereas (2) gives '1 Year, 3 Days'
* @param array Set of Name => Seconds pairs to use as divisors, ie array('Year' => 31536000)
* @return string Formatted interval
*/
function format_interval($seconds, $precision = -1, $separator = ', ', $divisors = NULL)
{

    // Default set of divisors to use
    if(!isset($divisors)) {
        $divisors = Array(
            'Year'      => 31536000, 
            'Month'     => 2628000, 
            'Day'       => 86400, 
            'Hour'      => 3600, 
            'Minute'    => 60, 
            'Second'    => 1);
    }

    arsort($divisors);

    // Iterate over each divisor
    foreach($divisors as $name => $divisor)
    {
        // If there is at least 1 of thie divisor's time period
        if($value = floor($seconds / $divisor)) {
            // Add the formatted value - divisor pair to the output array.
            // Omits the plural for a singular value.
            if($value == 1)
                $out[] = "$value $name";
            else
                $out[] = "$value {$name}s";

            // Stop looping if we've hit the precision limit
            if(--$precision == 0)
                break;
        }

        // Strip this divisor from the total seconds
        $seconds %= $divisor;
    }

    // FIX
    if (!isset($out)) {
        $out[] = "0" . $name;
    }

    var_dump($out);
    // Join the value - divisor pairs with $separator between each element
    return implode($separator, $out);

}
链接地址: http://www.djcxy.com/p/58978.html

上一篇: 如何计算“时间前”在PHP中

下一篇: 日期到时间? 不使用strtime?