php相对时间问题 [英] php relative time problems

查看:87
本文介绍了php相对时间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个函数来计算自某一日期以来所经过的相对时间,





  function nicetime($ date){
if(empty($ date)){
return没有提供日期;
}

$ periods = array(second,minute,hour,day,week,month,year,decade) ;
$ lengths = array(60,60,24,7,4.35,12,10);

$ now = time();
$ unix_date = strtotime($ date);

//检查日期的有效性
if(empty($ unix_date)){
returnBad date;
}

//是未来日期或过去日期
if($ now> $ unix_date){
$ difference = $ now - $ unix_date;
$ tense =ago;

} else {
$ difference = $ unix_date - $ now;
$ tense =from now;
}

for($ j = 0; $ difference> = $ lengths [$ j]& $ j< count($ lengths)-1; $ j ++) {
$ differences / = $ lengths [$ j];
}

$ difference = round($ difference);

if($ difference!= 1){
$ periods [$ j]。=s;
}

return$ difference $ periods [$ j] {$ tense};
}

我传递函数的日期为2011-01-16 12: 30,但是我得到不好的日期返回,这意味着 $ unix_date 是空的,但是如果我死在函数中,我得到 $ date 传递给函数,但是它返回一个(在它之前,下面是我如何调用该方法。



echo nicetime(date('Y:m:d G:i',$ a ['created_at']))

方案

  echo nicetime(2011-01-16 12:30); 

对我来说很合适我建议你确保

  $ a ['created_at '] 

给你你的东西。

  $ dtDate = $ a ['created_at']; 
$ strDate = date('Y:m:d G:i',$ dtDate) ;
echo $ strDate;
echo nicetime($ strDate);



肯定$ strDate是你认为的。


I have this function to work out the relative time that has elapsed since a certain date,

function nicetime($date) {
    if(empty($date)) {
        return "No date provided";
    }

    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");

    $now             = time();
    $unix_date         = strtotime($date);

       // check validity of date
    if(empty($unix_date)) {   
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {   
        $difference     = $now - $unix_date;
        $tense         = "ago";

    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }

    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }

    $difference = round($difference);

    if($difference != 1) {
        $periods[$j].= "s";
    }

    return "$difference $periods[$j] {$tense}";
}

I am passing the function a date as "2011-01-16 12:30", however I am getting bad date returned which means that $unix_date is empty however if I die in the function I get the $date that is passed to the function however it gets return with a ( before it, below is how I am calling the method.

echo nicetime(date('Y:m:d G:i', $a['created_at']))

解决方案

echo nicetime("2011-01-16 12:30");

works fine for me. I would suggest you ensure

 $a['created_at']

is giving you what you thing it is. Maybe try

$dtDate = $a['created_at'];
$strDate = date('Y:m:d G:i', $dtDate );
echo $strDate;
echo nicetime($strDate);

Make sure $strDate is what you think it is.

这篇关于php相对时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆