php – Symfony / Doctrine Dateinterval(持续时间)如何将其存储
发布时间:2020-12-13 21:48:15 所属栏目:PHP教程 来源:网络整理
导读:我有一个问题,我正在编写一个对服务有某种保留的应用程序.该服务有持续时间,例如: A massage that takes 1 hour and 15 minutes 然后我为这项服务预订系统.我在预订时需要计算结束日期时间. 所以我在我的数据库中有一个“开始”的日期时间,我不知道如何存储
我有一个问题,我正在编写一个对服务有某种保留的应用程序.该服务有持续时间,例如:
然后我为这项服务预订系统.我在预订时需要计算结束日期时间. 所以我在我的数据库中有一个“开始”的日期时间,我不知道如何存储持续时间.因此,在预订后,我可以很容易地说,这将在其他一些日期时间结束. 我希望我很清楚. 问题是如何在数据库中存储持续时间以及如何用它来增加开始日期,所以我对时区等没有任何问题. 感谢帮助! 解决方法
这是一种只使用
PHP(而不是使用SQL)的方法,时间以秒为单位进行管理以简化计算:
$reservation = new Reservation(); // Your entity object $startDate = new DateTime('now'); $endDate = $startDate; $endDate->modify('+'.4500.' seconds'); // Update the end time with the duration of the service $reservation->setStartDate($startDate); $reservation->setEndDate($endDate); // Save the reservation $em = $this->getDoctrine()->getManager(); $em->persist($reservation); $em->flush(); 编辑1: 要回答您的时区问题,最容易(我认为)是使用时间戳!在显示时,时间戳将转换为时区日期.从日期时间获取时间戳时,它是相同的,它是根据机器的时区计算的.所以时间戳在时区之间共享^^ 这里的片段编辑: // ... // Save timestamp instead of datetime $reservation->setStartTimestamp($startDate->getTimestamp()); $reservation->setEndTimestamp($endDate->getTimestamp()); // ... 编辑2: 要回答您的评论,如果您更改了持续时间,只需将持续时间保存在数据库中即可. // First save $reservation->setDuration(4000); // seconds 编辑持续时间时: // Duration of a reservation change // <- Load the $reservation to edit $date = new DateTime(); $date->setTimestamp($reservation->getStartTimestamp()); // Got start date $reservation->setDuration($reservation->getDuration() + 2000); // For example,duration is increased of 2000 second $endDate = $date; $endDate->modify('+'.$reservation->getDuration().' seconds'); // Use start date and recalculate new end date $reservation->setEndTimestamp($endDate->getTimestamp()); // <- Then update $reservation with a persist (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |