Error date_diff PHP

3

I am doing a test, with an Oracle database query (writing sysdate, result: 13/09/2016 13:24:44) and returning me in PHP. I need to calculate the difference in hours and I was trying the function below:

$date_a = new DateTime($p_fim[$passo]);
$date_b = new DateTime($p_inicio[$passo]);

$interval = date_diff($date_a,$date_b);
echo $interval->format('%h:%i:%s');

But you are returning the error below:

  

Fatal error: Uncaught exception 'Exception' with message   'DateTime :: __ construct (): Failed to parse time string (9/13/2016   13:24:44) at position 0 (1): Unexpected character '

Browsing the same OS all use the 2016-09-13 format. That's right? Do I need to convert before passing the value?

    
asked by anonymous 13.09.2016 / 18:43

1 answer

6

These are the supported types, according to the documentation:

const string ATOM = "Y-m-d\TH:i:sP" ;
const string COOKIE = "l, d-M-Y H:i:s T" ;
const string ISO8601 = "Y-m-d\TH:i:sO" ;
const string RFC822 = "D, d M y H:i:s O" ;
const string RFC850 = "l, d-M-y H:i:s T" ;
const string RFC1036 = "D, d M y H:i:s O" ;
const string RFC1123 = "D, d M Y H:i:s O" ;
const string RFC2822 = "D, d M Y H:i:s O" ;
const string RFC3339 = "Y-m-d\TH:i:sP" ;
const string RSS = "D, d M Y H:i:s O" ;
const string W3C = "Y-m-d\TH:i:sP" ;

link

Your case does not have an officially standardized format, so you will have to provide an acceptable format.

See an example of how to use the method DateTime::createFromFormat()

$date_a = DateTime::createFromFormat('d/m/Y H:i:s', '13/09/2016 13:24:44');
$date_b = DateTime::createFromFormat('d/m/Y H:i:s', '13/09/2016 20:24:44');
$interval = date_diff($date_a,$date_b);
echo $interval->format('%H:%i:%s');

Being more specific to your case,

$date_a = new DateTime('d/m/Y H:i:s', $p_fim[$passo]);
    
13.09.2016 / 19:38