php返回日期/时间问题
我在下面有一个简单的函数,它获取秒数并返回它,如“2分钟前”、“9小时前”等 如果我在“relativedate()”函数上传递1,它将只返回1秒,以此类推。众所周知,60*60*24*7*30或18144000秒=1个月。因此,如果我在参数上传递值18144000,它应该返回1个月。但是,如果我在参数上传递值,比如在relativedate()上传递值231440000,它应该超过一年,它将返回“13个月”,而不是1年php返回日期/时间问题,php,date,time,Php,Date,Time,我在下面有一个简单的函数,它获取秒数并返回它,如“2分钟前”、“9小时前”等 如果我在“relativedate()”函数上传递1,它将只返回1秒,以此类推。众所周知,60*60*24*7*30或18144000秒=1个月。因此,如果我在参数上传递值18144000,它应该返回1个月。但是,如果我在参数上传递值,比如在relativedate()上传递值231440000,它应该超过一年,它将返回“13个月”,而不是1年 function relativedate($secs) {
function relativedate($secs) {
$second = 1;
$minute = 60;
$hour = 60*60;
$day = 60*60*24;
$week = 60*60*24*7;
$month = 60*60*24*7*30;
$year = 60*60*24*7*30*365;
if ($secs <= 0) { $output = "now";
}elseif ($secs > $second && $secs < $minute) { $output = round($secs/$second)." second";
}elseif ($secs >= $minute && $secs < $hour) { $output = round($secs/$minute)." minute";
}elseif ($secs >= $hour && $secs < $day) { $output = round($secs/$hour)." hour";
}elseif ($secs >= $day && $secs < $week) { $output = round($secs/$day)." day";
}elseif ($secs >= $week && $secs < $month) { $output = round($secs/$week)." week";
}elseif ($secs >= $month && $secs < $year) { $output = round($secs/$month)." month";
}elseif ($secs >= $year && $secs < $year*10) { $output = round($secs/$year)." year";
}else{ $output = " more than a decade ago"; }
if ($output <> "now"){
$output = (substr($output,0,2)<>"1 ") ? $output."s" : $output;
}
return $output;
}
echo relativedate(60); // 1 minute
函数相对值($secs){
$second=1;
$minute=60;
$hour=60*60;
$day=60*60*24;
$week=60*60*24*7;
$month=60*60*24*7*30;
$year=60*60*24*7*30*365;
如果($secs$second&$secs<$minute){$output=round($secs/$second)。“second”;
}elseif($secs>=$minute&&$secs<$hour){$output=round($secs/$minute)。“minute”;
}elseif($secs>=$hour&&$secs<$day){$output=round($secs/$hour)。“hour”;
}elseif($secs>=$day&&$secs<$week){$output=round($secs/$day)。“day”;
}elseif($secs>=$week&&$secs<$month){$output=round($secs/$week)。“week”;
}elseif($secs>=$month&&$secs<$year){$output=round($secs/$month)。“month”;
}elseif($secs>=$year&&$secs<$year*10){$output=round($secs/$year)。“年”;
}否则{$output=“十多年前”}
如果($output“now”){
$output=(substr($output,0,2)“1”)?$output。“s”:$output;
}
返回$output;
}
回波相对值(60);//1分钟
您的年度计算是365个月,而不是12个月为什么不拆分并检查>=12(也要注意下一步..意味着26个月应该是2年20个月,所以您也需要迭代thr…只需检查闰年计数如何..在处理日期/时间时也要为其编码)你的年度计算是365个月,而不是12个月。更多的是评论而不是回答?谢谢,我没听清楚,我想的是几天