将php mysql时间转换为秒前
我转换时间有问题。出于某种原因,它似乎是默认回到1970年,我相信这是unix默认时间。我正在使用这段代码,当我运行这个程序时,我得到的是42年,而不是2年。我错过了什么。请帮忙 这是密码将php mysql时间转换为秒前,php,mysql,datetime,Php,Mysql,Datetime,我转换时间有问题。出于某种原因,它似乎是默认回到1970年,我相信这是unix默认时间。我正在使用这段代码,当我运行这个程序时,我得到的是42年,而不是2年。我错过了什么。请帮忙 这是密码 <?php function convertime($ptime) { $etime = time() - $ptime; if($etime < 60) { return 'less than minute ag';
<?php
function convertime($ptime) {
$etime = time() - $ptime;
if($etime < 60) {
return 'less than minute ag';
}
$a = array(12 * 30 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60 => 'month', 24 * 60 * 60 => 'day', 60 * 60 => 'hour', 60 => 'minute' // 1 => 'second'
);
foreach($a as $secs => $str) {
$d = $etime / $secs;
if($d >= 1) {
$r = round($d);
return $r . ' ' . $str . ($r > 1 ? 's' : '');
}
}
}
?>
<?php
$ctime = 2009-02-23 10:09:00 //time from mysql
echo convertime($ctime);
?>
在时间戳周围包裹strotime()
:
$ctime = strtotime('2009-02-23 10:09:00'); //time from mysql
您需要将时间戳与时间戳进行比较。
<?php
function convertime($ptime) {
$etime = time() - $ptime;
if($etime < 60) {
return 'less than minute ag';
}
$a = array(12 * 30 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60 => 'month', 24 * 60 * 60 => 'day', 60 * 60 => 'hour', 60 => 'minute' // 1 => 'second'
);
foreach($a as $secs => $str) {
$d = $etime / $secs;
if($d >= 1) {
$r = round($d);
return $r . ' ' . $str . ($r > 1 ? 's' : '');
}
}
}
?>
<?php
$ctime = strtotime('2009-02-23 10:09:00'); //time from mysql
echo convertime($ctime);
?>
试试看你是否真的将
2009-02-23 10:09:00
传递给你的函数?如果是这样,请用strotime()
包裹它。没问题,很高兴我能帮上忙。也可以随意投票<代码>:D一个简单的问题,为什么它说3年而不是2让我看看;我想这是因为您的比较foreach
循环没有按预期工作。试试这个:我想这是因为您使用的是round()
,而不是floor()
,它是四舍五入的。顺便说一句,看看数组结果。@user836910-你是认真的吗?你要选择另一个答案作为正确答案吗?(顺便说一句,您只能有一个。)