• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

时间格式转换问题

php 搞代码 3年前 (2022-01-23) 18次浏览 已收录 0个评论
文章目录[隐藏]

2013-11-01 22:26:56
怎样把上面的时间格式转换成3秒前或3天前这种格式显示

回复讨论(解决方案)

$time='2013-11-01 22:26:56';echo date('Y-m-d H:i:s',strtotime($time)-3); //3秒前echo "<br>";echo date('Y-m-d H:i:s',strtotime($time)-3*24*60*60); //3天前

function time_since($since) {    $chunks = array(        array(60 * 60 * 24 * 365 , 'year'),        array(60 * 60 * 24 * 30 , 'month'),        array(60 * 60 * 24 * 7, 'week'),        array(60 * 60 * 24 , 'day'),        array(60 * 60 , 'hour'),        array(60 , 'minute'),        array(1 , 'second')    );    for ($i = 0, $j = count($chunks); $i < $j; $i++) {        $seconds = $chunks[$i][0];        $name = $chunks[$i][1];        if (($count = floor($since / $seconds)) != 0) {            break;        }    }    $print = ($count == 1) ? '1 '.$name : "$count {$name}s";    return $print;}

代码如下:


网页显示的时间格式是:1382972373.怎样把这种时间格式转换成3秒前或3天前这种格式显示

这种更方便,不用转了:

$time='1382972373';echo date('Y-m-d H:i:s',$time-3); //3秒前echo "<br>";echo date('Y-m-d H:i:s',$time-3*24*60*60); //3天前

如果想结果也显示时间戳,就不用date转:

$time='1382972373';echo $time-3; //3秒前echo "<br>";echo $time-3*24*60*60; //3天前

[/code]

代码如下:


网页显示的时间格式是:1382972373.怎样把这种时间格式转换成3秒前或3天前这种格式显示

你说的是显示为weibo那种多少分钟前发布的吧?我发的那个函数就可以。

是的,就象weibo那种多少分钟前发布,那怎样调用?

<script language=”javascript”>
function time_since($home.regtime) {
$chunks = array(
array(60 * 60 * 24 * 365 , ‘year’),
array(60 * 60 * 24 * 30 , ‘month’),
array(60 * 60 * 24 * 7, ‘week’),
array(60 * 60 * 24 , ‘day’),
array(60 * 60 , ‘hour’),
array(60 , ‘minute’),
array(1 , ‘second’) );
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($home.regtime / $seconds)) != 0) {
break;
}
}
$print = ($count == 1) ? ‘1 ‘.$name : “$count {$name}s”;
return $print;
}
</script>
添加这些代码无效果,网页无任何反应,该怎样调用?

汗,直接把发布的时候和当前时间对比下,不就得出多长时间之前发布的了吗,之前都这么计算都发的那么清楚了

汗,直接把发布的时候和当前时间对比下,不就得出多长时间之前发布的了吗,之前都这么计算都发的那么清楚了
引用你那段代码也不行,如下:

$time=’1382972373′; echo date(‘Y-m-d H:i:s’,$time-3); //3秒前 echo “
“; echo date(‘Y-m-d H:i:s’,$time-3*24*60*60); //3天前
结果页显示的是源代码,根本没编绎.

这个是你用什么框架,在模板文件里面写了吧!这个只是告诉你方法,具体肯定要遵循框架的规则来来写,每个框架的标签规则也不一样阿

模板源代码如下:


那怎样加入你那段代吗呢?试了次多不行

<script language=”javascript”>
function time_since($home.regtime) {
$chunks = array(
array(60 * 60 * 24 * 365 , ‘year’),
array(60 * 60 * 24 * 30 , ‘month’),
array(60 * 60 * 24 * 7, ‘week’),
array(60 * 60 * 24 , ‘day’),
array(60 * 60 , ‘hour’),
array(60 , ‘minute’),
array(1 , ‘second’) );
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($home.regtime / $seconds)) != 0) {
break;
}
}
$print = ($count == 1) ? ‘1 ‘.$name : “$count {$name}s”;
return $print;
}
</script>
添加这些代码无效果,网页无任何反应,该怎样调用?
人家的函数是PHP的,你用到JS里,太有才了

function sgmdate($dateformat, $format=0) {	$result = '';	if($format) {		$time = time() - $dateformat;		if($time > 24*3600) {			$result = date('Y-m-d',$dateformat);		} elseif ($time > 3600) {			$result = intval($time/3600).'小时前';		} elseif ($time > 60) {			$result = intval($time/60).'分钟前';		} elseif ($time > 0) {			$result = $time.'秒前前';		} else {			$result = '刚刚';		}	} else {		$result = date('Y-m-d',$dateformat);	}	return $result;}

这个放在你的一个php文件里,你调用该php文件,然后在你的模版中调用:

就OK

function sgmdate($dateformat, $format=0) {	$result = '';	if($format) {		$time = time() - $dateformat;		if($time > 24*3600) {			$result = date('Y-m-d',$dateformat);		} elseif ($time > 3600) {			$result = intval($time/3600).'小时前';		} elseif ($time > 60) {			$result = intval($time/60).'分钟前';		} elseif ($time > 0) {			$result = $time.'秒前前';		} else {			$result = '刚刚';		}	} else {		$result = date('Y-m-d',$dateformat);	}	return $result;}

这个放在你的一个php文件里,你调用该php文件,然后在你的模版中调用:

就OK
还是不明白”这个放在你的一个php文件里,你调用该php文件”这句话,怎样调用php文件呢?

你使用的是什么模板引擎?(看上去像是 Smarty)
模板引擎都可以自定义模板函数,但不同的模板引擎对于自定义函数的约定与调用是不同的

你使用的是什么模板引擎?(看上(本文来)源gaodaimacom搞#^代%!码&网(

搞gaodaima代码

去像是 Smarty)
模板引擎都可以自定义模板函数,但不同的模板引擎对于自定义函数的约定与调用是不同的
是 Smarty模板引擎,那怎样调用?

http://www.baidu.com/s?wd=Smarty%E6%A8%A1%E6%9D%BF%E5%87%BD%E6%95%B0&ie=utf-8

http://www.baidu.com/s?wd=Smarty%E6%A8%A1%E6%9D%BF%E5%87%BD%E6%95%B0&ie=utf-8
看了还是不太明白,能讲详细点吗?比如下面这段代码修改成 Smarty模板函数要怎样修改?
function sgmdate($dateformat, $format=0) { $result = ”; if($format) { $time = time() – $dateformat; if($time > 24*3600) { $result = date(‘Y-m-d’,$dateformat); } elseif ($time > 3600) { $result = intval($time/3600).’小时前’; } elseif ($time > 60) { $result = intval($time/60).’分钟前’; } elseif ($time > 0) { $result = $time.’秒前前’; } else { $result = ‘刚刚’; } } else { $result = date(‘Y-m-d’,$dateformat); } return $result; }

还有在模板代码中如何调用,怎样修改,模板源代码如下:


http://www.baidu.com/s?wd=Smarty%E6%A8%A1%E6%9D%BF%E5%87%BD%E6%95%B0&ie=utf-8
看了还是不太明白,能讲详细点吗?比如下面这段代码修改成 Smarty模板函数要怎样修改?
function sgmdate($dateformat, $format=0) { $result = ”; if($format) { $time = time() – $dateformat; if($time > 24*3600) { $result = date(‘Y-m-d’,$dateformat); } elseif ($time > 3600) { $result = intval($time/3600).’小时前’; } elseif ($time > 60) { $result = intval($time/60).’分钟前’; } elseif ($time > 0) { $result = $time.’秒前前’; } else { $result = ‘刚刚’; } } else { $result = date(‘Y-m-d’,$dateformat); } return $result; }

还有在模板代码中如何调用,怎样修改,模板源代码如下:


Smarty模板引擎
那你就在你的PHP文件里找到
$tpl -> assign(“home”, $home);
在这个之前加下面这么一句就OK:
$home[‘regtime’]=sgmdate($home[‘regtime’],1);


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:时间格式转换问题

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址