-1

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?

Mario Galic
  • 47,285
  • 6
  • 56
  • 98

3 Answers3

2

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
SScotti
  • 2,158
  • 4
  • 23
  • 41
1

Use momentjs (https://momentjs.com/docs/#/displaying/difference/)

var departureRaw = $(".departure").val();
var departureDate = moment(departureRaw);

var returnRaw = $(".return").val();
var returnDate = moment(returnRaw);

var difference = departureDate.diff(returnDate, 'days');
Brandon Kiefer
  • 329
  • 2
  • 9
1

for php, as mentionned here and here you can use ->diff() function. I don't think than Symfony have any function for that