开发者

date to time ago ? without useing strtime?

开发者 https://www.devze.com 2023-01-12 10:51 出处:网络
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 min开发者_运维问答utes x seconds ago ? but without useing the str_time ( php 5.3.3 )

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 min开发者_运维问答utes 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);

}


I found the following function when searching on google, i tested the code out and it seems to work perfectly.

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;
  }

you would then call it as

echo ago($time);


Fairly simply - you need start and current timestamps. For instance, to see the number of minutes passed from a start date of this evening (8/22/2010 9pm PST):

$start = strtotime('8/22/2010 9pm');
printf('%d minutes ago', (time() - $start) / 60);
// My Output: 36 minutes ago. Will vary, depending on the current time/zone.

The 60 divisor is to get minutes. For seconds there'd be no division. For hours, days, years, etc. use different divisors. The following solution has a sample function for formatting timestamps:

How to display "12 minutes ago" etc in a PHP webpage?

0

精彩评论

暂无评论...
验证码 换一张
取 消