tags:

views:

74

answers:

4

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

A: 

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

codaddict
updated. i mean like that.
Adam Ramadhan
A: 

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:

http://stackoverflow.com/questions/18685/how-to-display-12-minutes-ago-etc-in-a-php-webpage

pygorex1
yup it work but it does use strtotime as my question is not useing strtotime :| its strict error . any more way ?
Adam Ramadhan
Have you set your timezone correctly?
Leon
The `strtotime` function has been available since PHP 4 - why not use it? http://us.php.net/strtotime
pygorex1
yes see the php docs.
Adam Ramadhan
+2  A: 
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);

}
Adam Ramadhan
this works, thanks guys :) and viper-7
Adam Ramadhan
+1  A: 

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);
krike
wew its hangs my computer for($j = 0; $difference >= $lengths[$j]; $j++) , dont know but its seems Undefined offset unlimited counts :|
Adam Ramadhan
Not sure what you mean. I just did a quick google, tested it out in one of my projects and it worked without a problem giving the right time.
krike