Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将php mysql时间转换为秒前_Php_Mysql_Datetime - Fatal编程技术网

将php mysql时间转换为秒前

将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';

我转换时间有问题。出于某种原因,它似乎是默认回到1970年,我相信这是unix默认时间。我正在使用这段代码,当我运行这个程序时,我得到的是42年,而不是2年。我错过了什么。请帮忙 这是密码

<?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-你是认真的吗?你要选择另一个答案作为正确答案吗?(顺便说一句,您只能有一个。)