Php 到现在为止?不使用strtime?
好的,我有个约会Php 到现在为止?不使用strtime?,php,time,Php,Time,好的,我有个约会 date('Y-m-d H:i:s'); 我们怎样才能使它像0秒前或x年x月x日x分x秒前?但不使用stru时间(PHP5.3.3) 编辑* 我的意思是某年某月某日几分几秒前。 像facebook或stackoverflow(更新。我的意思是像那样。-亚当·拉马丹2分钟前edit) 编辑* 有人说要利用时间 两分钟前 谢谢 亚当·拉马丹(Adam Ramadhan)为什么不从time()的输出中减去30 time()返回从历元开始以秒为单位测量的当前时间,因此从中减去30将给
date('Y-m-d H:i:s');
我们怎样才能使它像0秒前或x年x月x日x分x秒前?但不使用stru时间(PHP5.3.3)
编辑*
我的意思是某年某月某日几分几秒前。
像facebook或stackoverflow(更新。我的意思是像那样。-亚当·拉马丹2分钟前
edit)
编辑*
有人说要利用时间
两分钟前
谢谢
亚当·拉马丹(Adam Ramadhan)为什么不从
time()的输出中减去30
time()
返回从历元开始以秒为单位测量的当前时间,因此从中减去30
将给出时间30
秒前的时间戳非常简单-您需要开始时间戳和当前时间戳。例如,要查看从今天晚上(太平洋标准时间2010年8月22日晚上9点)的开始日期起经过的分钟数:
60除数等于分钟。几秒钟内不会有分裂。对于小时、天、年等,使用不同的除数。以下解决方案具有用于格式化时间戳的示例函数:
我在谷歌上搜索时发现了以下功能,我测试了代码,它似乎工作得很好
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);
你应该试试这个(看起来很像克里克的答案,但这个是固定的!)
记住,这适用于这种时间格式----
这是肯定的工作,它是伟大和重量轻的脚本!欲了解更多信息,请访问此链接--
希望它能起作用 是的,它可以工作,但它确实使用了strotime,因为我的问题不是使用strotime:|它的错误很严重。还有其他方法吗?strotime
函数从PHP4开始就可用了-为什么不使用它呢?wew its挂起我的电脑($j=0;$difference>=$length[$j];$j++),不知道,但它似乎未定义偏移量无限计数:|不确定你的意思。我只是做了一个快速的谷歌,在我的一个项目中进行了测试,在给出正确的时间时,它毫无问题地工作了。如果你有比几年前更老的东西,它会显示“3年前”。双“s”。需要将$periods数组中的“年”更改为“年”。的可能重复项
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);
}
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);
<?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!