日期到时间? 不使用strtime?

好的,我有个约会

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

我们怎么能使它像0秒前或x年x月x日x分钟x秒前? 但不使用str_time(php 5.3.3)

编辑*

我的意思是some.date - time.now =几年前某个月的某个月。 像facebook或者stackoverflow(更新,我的意思是这样 - Adam Ramadhan 2 mins ago编辑)

编辑*有人说使用sktime?

2分钟前。

谢谢

亚当拉马丹


你应该试试这个(看起来完全像克劳克的答案,但这个是固定的!)

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

只要做到这一点---

echo ago($timeofthethingyouwant);

请记住,这与这种时间格式有关 -

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

这是肯定的,它是伟大而轻量级的脚本! 欲了解更多关于此链接的信息,

http://drupal.org/node/61565

希望它有效!


为什么不从time()的输出中减去30

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

time()返回当前时间,它以时间以秒计算,所以从中减去30给出30秒前的时间的时间戳


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/58977.html

上一篇: date to time ago ? without useing strtime?

下一篇: show the message time in the inbox php