php相对时间问题

编程入门 行业动态 更新时间:2024-10-25 22:27:33
本文介绍了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相对时间问题

本文发布于:2023-10-24 09:46:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523615.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!