-2
    $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';
    }

Tryed to use string formats, but this is showing right results for now in print_r, missing something?

  • 1
    You're trying to subtract strings. Turn them into `DateTime` objects and use `diff` to calculate. – El_Vanja Feb 22 '21 at 00:05

1 Answers1

0

use 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');

or use $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');

otherwise use Unix timestamp format like 1613953162

so you can use

$diffrent_in_second = 1613953162-1613953151 // 11 seconds
Raskul
  • 1,674
  • 10
  • 26
  • It's working now, how I can hide the values 0 years, 0 months, 0 days, 0 minutes? `$datetime1 = new DateTime("$fileddataname"); $datetime2 = new DateTime('now'); $interval = $datetime1->diff($datetime2); $elapsed = $interval->format('%y anos %m meses %a dias %h horas %i minutos %s segundos'); echo $elapsed;` Thank you – André Santos Feb 22 '21 at 01:29