Convert date difference to string

Asked

Viewed 303 times

2

I’m calculating in PHP the difference between two datetimes (beginning, end), from a POST, need to fill out a campo(time) in the format(H:i:s) via INSERT in the MYSQL.

I get the result of the difference, but I can’t use it because by insert I get an error saying that the difference field is not converted to string:

Error: "(Object of class Dateinterval could not be converted to string)".

If it is the error of string same, how do I perform the conversion? Is there any other way to do this in the PHP?

$inicio = new DateTime($data_inicio); 
$fim   = new DateTime($data_fim);

$duracao = $inicio->diff($fim);
$duracao = $duracao->format('%h:%i:%s');

$sql = "INSERT into horarios VALUES ('$duracao')";

Thank you for your partnership

P.S.: In case you already have this doubt clarified in another post inform me, but I did a research first and I’m hitting myself a bit.

3 answers

3


A possibility, using simple subtraction and integers:

$date1 = strtotime( '10/04/2016 15:03' );
$date2 = strtotime( '10/04/2016 16:28' );

$duracao = date( 'H:i:s', abs( $date2 - $date1 ) );

// Aqui usa o valor como necessitar:
$sql = "INSERT INTO horarios VALUES ('$duracao')";

See working on IDEONE.

Important note that when the date is separated by traces, PHP already interprets as DD-MM-AAAA, and with bars as MM/DD/AAAA. If you need it, you can change it like this:

strtotime(str_replace('/', '-', '11/04/2016 18:10'));


If you need to compare the time difference with the current date

Enough instead of using strtotime( "$data $hora" );, choose one of these functions

$date2 = mktime(); // Use essa se quiser hora local
$date2 = time();   // Ou essa se quiser UTC
  • If you need double quotes, you can do $sql = 'INSERT INTO horarios VALUES ("'.$duracao.'")';

  • Simple and solved. did not know yet the existence of abs, is there any negative or error that can be generated using this method ? for example if I calculate a duration between months.

  • 1

    @Cleverson the abs simply returns the absolute value (takes the "negative" of the number), has no side effect. Duration problems at very large intervals would be caused by integer arithmetic, but this affects any application, regardless of the technique used.

2

Hugo, make sure that your field in the database is time or something like that, from version 5.6 of php it is possible for time objects to talk directly to the database, please, if your table is string which is not recommended, you should explore the Dateinterval object format it for string and add it to the database, if you cannot and if you want, you can also use the library Chronus.

  • Very interesting library, I will definitely start using it. Valew Sebastiao

0

Use with the date_diff function:

$differenceFormat = "%H:%i:%s"

$datetime1 = date_create($data_inicio);
$datetime2 = date_create($data_fim);

$interval = date_diff($datetime1, $datetime2);

$sql = "INSERT into horarios VALUES ('$interval->format($differenceFormat);')";
  • Hugo, I tried to apply but when performing the Insert generates an error: "Undefined Property: Dateinterval::$format".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.