Search code examples
javascriptphptwigsymfony4twig-extension

How to get the difference between two dates with php and symfony 4?


I am developing a holiday management project. There is an entity called Conge which includes date_departure, date_retrun and period. I made a form and the view. I would like to set the difference between the date of departure and the date of return. For example, if the user chooses dates from '04-05-2019' to '04-08-2019', how can I get and display the difference in days using javascript, php, and symfony4?


Solution

  • If you are always going to use the m-d-Y format for your dates you can use just PHP to give you the number of days difference.

    $departure ="04-05-2019";
    $arrival = "04-08-2019";
    $departure = DateTime::createFromFormat('m-d-Y', $departure)->getTimestamp();
    $arrival = DateTime::createFromFormat('m-d-Y', $arrival)->getTimestamp();
    echo ($arrival - $departure) / (24*60*60);  // 86400 might save some math