函数来格式化时间差异 [英] function that formats the time difference
问题描述
我试图创建一个将日期与当前时间进行比较的函数,并返回一个格式良好的字符串。
我已经用匆忙的方式编写了一些代码并且它可以工作,但我试图找到这是一种更有效的方式。这是我的代码:
函数_formatDate($ dateStr)
{
$ timestr =;
$ t = time() - strtotime($ dateStr);
if($ t <60){
$ timestr ={$ t} seconds ago;
}
elseif($ t <120){
$ timestr =大约一分钟前;
}
elseif($ t <3600){
$ minute = floor($ t / 60);
$ timestr ={$ minute} minutes ago;
}
elseif($ t <7200){
$ timestr =约一小时前;
}
elseif($ t <86400){
$ hour = floor($ t / 3600);
$ timestr ={$ hour} hours ago;
}
elseif($ t <172800){
$ timestr =一天前;
}
elseif($ t <2592000){
$ day = floor($ t / 86400);
$ timestr ={$ day} days ago;
}
elseif($ t <5184000){
$ timestr =大约一个月前;
}
else {
$ month = floor($ t / 2592000);
$ timestr ={$ month} months ago;
}
返回$ timestr;
}
从来没有失败,只要把Unix时间戳放进去,如果你在函数中使用第二个参数,可以是to条件。 > echo timeDiffrence('1300392875');
函数timeDiffrence($ from,$ to = null){
$ to =(($ to === null)?(time()):($ to)); $($ to $)=((is_int($ to))?($ to):( strtotime($ to)));
$ =((is_int($ from))?($ from):(strtotime($ from)));
$ units = array
(
y=> 29030400,//一年中的秒数(12个月)
month=> 2419200,//秒在一个月内(4周)
w=> 604800,//一周中的秒数(7天)
d=> 86400,//一天中的秒数)
h=> 3600,//一小时内的秒数(60分钟)
m=> 60,//一分钟内的秒数(60秒)
s=> 1 // 1秒
);
$ diff = abs($ from - $ to);
$ suffix =(($ from> $ to)?(from now):(ago));
foreach($ units为$ unit => $ mult)
if($ diff> = $ mult)
{
// $ and =(($ mult! = 1)?():(and)); $()$($)$($)$($)$($)$($)$($)$ ));
$ diff - = intval($ diff / $ mult)* $ mult;
}
$ output。=。$ suffix;
$ output = substr($ output,strlen());
if($ output =='go'|| $ output =='ago'){$ output ='几秒前';}
return $ output;
}
I am trying to create a function that compares a date with the current time, and returns a nicely formatted string.
I have written some code in haste and it works, but I am trying to find a more efficient way of doing it. Here is my code:
function _formatDate($dateStr)
{
$timestr = "";
$t= time() - strtotime($dateStr);
if($t < 60) {
$timestr = "{$t} seconds ago";
}
elseif ($t <120) {
$timestr = "about a minute ago";
}
elseif ($t < 3600) {
$minute = floor($t/60);
$timestr = "{$minute} minutes ago";
}
elseif ($t < 7200) {
$timestr = " about an hour ago";
}
elseif ($t < 86400) {
$hour = floor($t/3600);
$timestr = "{$hour} hours ago";
}
elseif ($t < 172800) {
$timestr = "a day ago";
}
elseif ($t < 2592000) {
$day = floor($t/86400);
$timestr = "{$day} days ago";
}
elseif ($t < 5184000){
$timestr = "about a month ago";
}
else {
$month = floor($t/2592000);
$timestr = "{$month} months ago";
}
return $timestr;
}
Some code I use and it's never failed, just put the Unix timestamp in, if you use a second argument in the function that can be the "to" condition
echo timeDiffrence('1300392875');
function timeDiffrence($from, $to = null){
$to = (($to === null) ? (time()) : ($to));
$to = ((is_int($to)) ? ($to) : (strtotime($to)));
$from = ((is_int($from)) ? ($from) : (strtotime($from)));
$units = array
(
"y" => 29030400, // seconds in a year (12 months)
"month" => 2419200, // seconds in a month (4 weeks)
"w" => 604800, // seconds in a week (7 days)
"d" => 86400, // seconds in a day (24 hours)
"h" => 3600, // seconds in an hour (60 minutes)
"m" => 60, // seconds in a minute (60 seconds)
"s" => 1 // 1 second
);
$diff = abs($from - $to);
$suffix = (($from > $to) ? ("from now") : ("ago"));
foreach($units as $unit => $mult)
if($diff >= $mult)
{
//$and = (($mult != 1) ? ("") : ("and "));
$output .= "".$and.intval($diff / $mult)."".$unit.((intval($diff / $mult) == 1) ? ("") : (""));
$diff -= intval($diff / $mult) * $mult;
}
$output .= " ".$suffix;
$output = substr($output, strlen(""));
if($output =='go' || $output ==' ago'){$output = 'A few secs ago';}
return $output;
}
这篇关于函数来格式化时间差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!