简体   繁体   English

如何用php和symfony 4获得两个日期之间的差异?

[英]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 . 有称为实体Conge包括date_departuredate_retrunperiod 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? 例如,如果用户选择从'04 -05-2019'到'04 -08-2019'的日期,如何使用javascript,php和symfony4获取并显示天数差异?

If you are always going to use the mdY format for your dates you can use just PHP to give you the number of days difference. 如果您总是要在日期中使用mdY格式,则可以仅使用PHP来指定天数差异。

$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

Use momentjs ( https://momentjs.com/docs/#/displaying/difference/ ) 使用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');

for php, as mentionned here and here you can use ->diff() function. 对于php,如此此处所述,您可以使用-> diff()函数。 I don't think than Symfony have any function for that 我认为Symfony对此没有任何功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM