【问题标题】:2 times, same format not calculating [duplicate]2次,相同的格式不计算[重复]
【发布时间】:2021-02-21 23:48:12
【问题描述】:
    $dbtime = $row["dateupload"];
    print_r($dbtime); //- DATE FORMAT: 2020-02-21 22:24:00
    echo("<br>");
    $servertime = date('Y-m-d h:i:s', time());
    print_r($servertime); //- DATE FORMAT: 2020-02-21 22:24:00
    
    $totaltime = $dbtime - $servertime;
    

    $etime = $totaltime; //-  A non well formed numeric value encountered in

    if ($etime < 1)
    {
        return '0 seconds';
    }

尝试使用字符串格式,但现在在 print_r 中显示正确的结果,遗漏了什么?

【问题讨论】:

  • 您正在尝试减去字符串。将它们转换为DateTime 对象并使用diff 进行计算。

标签: php mysql date math time


【解决方案1】:

使用 date_diff():

https://www.php.net/manual/en/datetime.diff.php

<?php
$origin = date_create('2009-10-11');
$target = date_create('2009-10-13');
$interval = date_diff($origin, $target);
echo $interval->format('%R%a days');

或使用 $object_of_DateTime->diff()

$origin = new DateTime('2009-10-11');
$target = new DateTime('2009-10-13');
$interval = $origin->diff($target);
echo $interval->format('%R%a days');

否则使用 Unix 时间戳格式,例如 1613953162

所以你可以使用

$diffrent_in_second = 1613953162-1613953151 // 11 seconds

【讨论】:

  • 它现在可以工作了,我如何隐藏 0 年、0 个月、0 天、0 分钟的值? $datetime1 = new DateTime("$fileddataname"); $datetime2 = new DateTime('now'); $interval = $datetime1-&gt;diff($datetime2); $elapsed = $interval-&gt;format('%y anos %m meses %a dias %h horas %i minutos %s segundos'); echo $elapsed;谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多