日期到时间?没有使用strftime?

时间:2010-08-23 03:39:08

标签: php time

好的我有约会

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

我们怎样才能让它像0秒前或x年x月x天x分钟x秒前一样?但是没有使用str_time(php 5.3.3)

编辑*

我的意思是some.date - time.now = some some some some some Some Some Some Some Some Some Some Some Some Some some 喜欢facebook或stackoverflow(更新。我的意思是那样。 - Adam Ramadhan 2 mins ago编辑)

编辑* 有人说使用sktime吗?

2分钟前。

由于

Adam Ramadhan

5 个答案:

答案 0 :(得分:2)

你应该尝试一下(看起来就像krike的答案,但这个是固定的!)

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

希望它有效!

答案 1 :(得分:1)

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

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

time()返回以纪元为单位的秒数测量的当前时间,因此从中减去30给出时间30秒前的时间戳

答案 2 :(得分:1)

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

}

答案 3 :(得分:1)

我在google上搜索时发现了以下功能,我测试了代码,看起来效果很好。

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($time);

答案 4 :(得分:0)

相当简单 - 您需要开始和当前时间戳。例如,要查看从今晚开始日期(2010年8月22日太平洋标准时间晚上9点)过去的分钟数:

$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.

60分数是得分。几秒钟就没有分裂了。数小时,数天,数年等使用不同的除数。以下解决方案具有用于格式化时间戳的示例函数:

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