PHP时间转换

PHP时间转换,php,time,unix-timestamp,Php,Time,Unix Timestamp,是否有PHP库可以将unix时间戳转换为以下格式: 8 hours and 17 minutes ago 不久前我制作了自己的脚本来完成这项工作,我只是无法追踪它,也不愿意花时间重新创建它(另外,我认为它可以做得更有效) 我的原始代码大致如下: $seconds = time() - $timestamp; $minutes = 0; $hours = 0; $days = 0; $weeks = 0; $months = 0; $years = 0; while($seconds >

是否有PHP库可以将unix时间戳转换为以下格式:

8 hours and 17 minutes ago
不久前我制作了自己的脚本来完成这项工作,我只是无法追踪它,也不愿意花时间重新创建它(另外,我认为它可以做得更有效)

我的原始代码大致如下:

$seconds = time() - $timestamp;

$minutes = 0;
$hours = 0;
$days = 0;
$weeks = 0;
$months = 0;
$years = 0;

while($seconds >= 60)
{
    $seconds -= 60;
    $minutes ++;

    if($minutes >= 60)
    {
        $minutes -= 60;
        $hours ++;

        if($hours >= 24)
        {
            // etc
        }
    }
}

if($hours < 1) return "$minutes minute" . ($minutes == 1 ? "" : "s")) . " and $seconds seconds" . ($seconds == 1 ? "" : "s"));
if($minutes < 1) return "$seconds second" . ($seconds == 1 ? "" : "s"));

// etc
$seconds=time()-$timestamp;
$minutes=0;
$hours=0;
$days=0;
$weeks=0;
$months=0;
$years=0;
而($s>=60)
{
$s-=60;
$minutes++;
如果($minutes>=60)
{
$minutes-=60;
$hours++;
如果($hours>=24)
{
//等
}
}
}
如果($hours<1)返回“$minutes minute”。($minutes==1?”:“s”))。“和$seconds”。($seconds==1?”:“s”);
如果($minutes<1)返回“$seconds”。($seconds==1?”:“s”);
//等
更好的是,如果有更好的方法来实现上述目标,我也会亲自尝试一下

/*
  Input parameter is the UNIX timestamp 
  of the starting date.
  The second parameter is optional -
  It's value is the ending date,
  also UNIX timestamp. If this
  parameter is not given, the 
  default date is current date.
*/
function duration($start,$end=null) {
 $end = is_null($end) ? time() : $end;

 $seconds = $end - $start;

 $days = floor($seconds/60/60/24);
 $hours = $seconds/60/60%24;
 $mins = $seconds/60%60;
 $secs = $seconds%60;

 $duration='';
 if($days>0) $duration .= "$days days ";
 if($hours>0) $duration .= "$hours hours ";
 if($mins>0) $duration .= "$mins minutes ";
 if($secs>0) $duration .= "$secs seconds ";

 //$duration = trim($duration);
 if($duration=='') $duration = '0 seconds';

 return $duration;
}
它不适合你最好使用下面给出的一些类

它不适合你最好使用下面给出的一些类

是,请检查

是的,请查收

函数TimeAgo($datefrom,$dateto=-1)
{
//默认值,并假设在该字段中传递了0
//这是一个错误,而不是时代
如果($datefrom=60&&$difference=60*60&&$difference=60*60*24&&$difference=60*60*24*7&&$difference<
60*60*24*30)
{
$interval=“ww”;
}
//如果差异在30天和365天之间
//月是一个很好的间隔,同样的事情
//如果恰好存在2月29日,则适用
//在两个日期之间,函数将返回
//一天的“不正确”值
其他($差异>=60*60*24*30&&$差异<
60*60*24*365)
{
$interval=“m”;
}
//如果差值大于或等于365
//天,返回年份。如果
//例如,您在4月28日调用该函数
//2008年在2007年4月29日过去。它将回来
//一年前,事实上(打呵欠!)不太可能
//一年过去了
其他(差异>=60*60*24*365)
{
$interval=“y”;
}
//根据时间间隔,确定
//两个日期之间的单位数
//从这一点开始,你会变得很难
//告诉我两者的区别
//如果$DateDiff
//返回值为1,请确保返回单数
//单位名称,例如“天”而不是“天”
交换机($间隔)
{
案例“m”:
$months_difference=floor$difference/60/60/24/
29);
而(mktime(日期(“H”),$DATEFORM),日期(“i”),$DATEFORM),
日期($s',$datefrom),日期($n',$datefrom)+(月差),
日期(“j”,“dateto美元”),日期(“Y”,“datefrom美元”)<$dateto)
{
$months_difference++;
}
$datediff=$months\u差;
//我们需要这个,因为这是可能的
//有一个“m”间隔和一个月
//差异为12,因为我们使用的是29天
//一个月后
如果($datediff==12)
{
$datediff--;
}
$res=($datediff==1)?“$datediff月前”:“$datediff”
“几个月前”;
打破
案例“y”:
$datediff=楼层($60/60/24/365);
$res=($datediff==1)?“$datediff一年前”:“$datediff”
多年前”;
打破
案例“d”:
$datediff=最低限额(差额/60/60/24);
$res=($datediff==1)?“$datediff前一天”:“$datediff”
“几天前”;
打破
案例“ww”:
$datediff=楼层($60/60/24/7);
$res=($datediff==1)?“$datediff一周前”:“$datediff”
“几周前”;
打破
案例“h”:
$datediff=最低限额(差额/60/60);
$res=($datediff==1)?“$datediff小时前”:“$datediff”
“数小时前”;
打破
案例“n”:
$datediff=最低限额(差额/60美元);
$res=($datediff==1)?“$datediff分钟前”:
“$datediff分钟前”;
打破
案例“s”:
$datediff=$difference;
$res=($datediff==1)?“$datediff秒前”:
“$datediff秒前”;
打破
}
返回$res;
}
函数TimeAgo($datefrom,$dateto=-1)
{
//默认值,并假设在该字段中传递了0
//这是一个错误,而不是时代
如果($datefrom=60&&$difference=60*60&&$difference=60*60*24&&$difference=60*60*24*7&&$difference<
60*60*24*30)
{
$interval=“ww”;
}
//如果差异在30天和365天之间
//月是一个很好的间隔,同样的事情
//如果恰好存在2月29日,则适用
//在两个日期之间,函数将返回
//一天的“不正确”值
其他($差异>=60*60*24*30&&$差异<
60*60*24*365)
{
$interval=“m”;
}
//如果差值大于或等于365
//天,返回年份。如果
//例如,您在4月28日调用该函数
//2008年在2007年4月29日过去。它将回来
//一年前,事实上(打呵欠!)不太可能
//一年过去了
其他(差异>=60*60*24*365)
{
$interval=“y”;
}
//根据时间间隔,确定
//两个日期之间的单位数
//从这一点开始,你会变得很难
//告诉我两者的区别
//如果$DateDiff
//返回值为1,请确保返回单数
//单位名称,例如“天”而不是“天”
交换机($间隔)
{
案例“m”:
$months_difference=floor$difference/60/60/24/
29);
而(mktime(日期(“H”),$DATEFORM),日期(“i”),$DATEFORM),
日期($s',$datefrom),日期($n',$datefrom)+(月差),
日期(“j”,“dateto美元”),日期(“Y”,“datefrom美元”)<$dateto)
{
$months_difference++;
}
$datediff=$months\u差;
//我们需要这个,因为这是可能的
//有一个“m”间隔和一个月
//差异为12,因为我们使用的是29天
//一个月后
如果($datediff==12)
{
$datediff--;
}
$res=($datediff==1)?“$datediff月前”:“$datediff”
“几个月前”;
打破
案例“y”:
$datediff=楼层($60/60/24/365);
$res=($datediff==1)?“$datediff一年前”:“$datediff”
多年前”;
打破
案例“d”:
$datediff=最低限额(差额/60/60/24);
$res=($datediff==1)?“$datediff前一天”:“$datediff”
“几天前”;
打破
案例“ww”:
$datediff=楼层($60/60/24/7);
$res=($datediff==1)?“$date
$datetime1= new DateTime();
$datetime2= new DateTime();
$datetime2->setTimestamp($timestamp);

$interval = $datetime2->diff($datetime1);
echo $interval->format('%a days %h hours and %i minutes ago');
function TimeAgo($datefrom,$dateto=-1)
{
// Defaults and assume if 0 is passed in that
// its an error rather than the epoch

if($datefrom<=0) { return "A long time ago"; }
if($dateto==-1) { $dateto = time(); }

// Calculate the difference in seconds betweeen
// the two timestamps

$difference = $dateto - $datefrom;

// If difference is less than 60 seconds,
// seconds is a good interval of choice

if($difference < 60)
{
$interval = "s";
}

// If difference is between 60 seconds and
// 60 minutes, minutes is a good interval
elseif($difference >= 60 && $difference<60*60)
{
$interval = "n";
}

// If difference is between 1 hour and 24 hours
// hours is a good interval
elseif($difference >= 60*60 && $difference<60*60*24)
{
$interval = "h";
}

// If difference is between 1 day and 7 days
// days is a good interval
elseif($difference >= 60*60*24 && $difference<60*60*24*7)
{
$interval = "d";
}

// If difference is between 1 week and 30 days
// weeks is a good interval
elseif($difference >= 60*60*24*7 && $difference <
60*60*24*30)
{
$interval = "ww";
}

// If difference is between 30 days and 365 days
// months is a good interval, again, the same thing
// applies, if the 29th February happens to exist
// between your 2 dates, the function will return
// the 'incorrect' value for a day
elseif($difference >= 60*60*24*30 && $difference <
60*60*24*365)
{
$interval = "m";
}

// If difference is greater than or equal to 365
// days, return year. This will be incorrect if
// for example, you call the function on the 28th April
// 2008 passing in 29th April 2007. It will return
// 1 year ago when in actual fact (yawn!) not quite
// a year has gone by
elseif($difference >= 60*60*24*365)
{
$interval = "y";
}

// Based on the interval, determine the
// number of units between the two dates
// From this point on, you would be hard
// pushed telling the difference between
// this function and DateDiff. If the $datediff
// returned is 1, be sure to return the singular
// of the unit, e.g. 'day' rather 'days'

switch($interval)
{
case "m":
$months_difference = floor($difference / 60 / 60 / 24 /
29);
while (mktime(date("H", $datefrom), date("i", $datefrom),
date("s", $datefrom), date("n", $datefrom)+($months_difference),
date("j", $dateto), date("Y", $datefrom)) < $dateto)
{
$months_difference++;
}
$datediff = $months_difference;

// We need this in here because it is possible
// to have an 'm' interval and a months
// difference of 12 because we are using 29 days
// in a month

if($datediff==12)
{
$datediff--;
}

$res = ($datediff==1) ? "$datediff month ago" : "$datediff
months ago";
break;

case "y":
$datediff = floor($difference / 60 / 60 / 24 / 365);
$res = ($datediff==1) ? "$datediff year ago" : "$datediff
years ago";
break;

case "d":
$datediff = floor($difference / 60 / 60 / 24);
$res = ($datediff==1) ? "$datediff day ago" : "$datediff
days ago";
break;

case "ww":
$datediff = floor($difference / 60 / 60 / 24 / 7);
$res = ($datediff==1) ? "$datediff week ago" : "$datediff
weeks ago";
break;

case "h":
$datediff = floor($difference / 60 / 60);
$res = ($datediff==1) ? "$datediff hour ago" : "$datediff
hours ago";
break;

case "n":
$datediff = floor($difference / 60);
$res = ($datediff==1) ? "$datediff minute ago" :
"$datediff minutes ago";
break;

case "s":
$datediff = $difference;
$res = ($datediff==1) ? "$datediff second ago" :
"$datediff seconds ago";
break;
}
return $res;
}