How to decrease this code in PHP?

Asked

Viewed 52 times

2

I am making a code in PHP that format the time that the site searches in the database. It is very simple. I wonder if there is a way to reduce it. Follow the code below:

$tempo = new DateTime($abre);
$abre = $tempo -> format('H:i');
$tempo = new DateTime($fecha);
$fecha = $tempo -> format('H:i');

1 answer

8


You can do so by adding parentheses when creating the object you can already call some class method.

$abre = (new DateTime($abre))->format('H:i');
$fecha = (new DateTime($fecha))->format('H:i');
  • Thank you very much!!!

Browser other questions tagged

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