【问题标题】:Symfony/Doctrine Dateinterval (time duration) how to store it in databaseSymfony/Doctrine Dateinterval(持续时间)如何将其存储在数据库中
【发布时间】:2013-06-02 13:30:05
【问题描述】:

我有一个问题,我正在编写一个对服务有某种保留的应用程序。该服务有一个持续时间,例如:

需要 1 小时 15 分钟的按摩

然后我为这项服务建立一个预订系统。在进行预订时,我需要计算结束日期时间。

所以我的数据库中有一个用于“开始”的日期时间,但我不知道如何存储持续时间。所以预订后我可以很容易地说这将在其他日期时间结束。

我希望我已经足够清楚了。

问题是如何在数据库中存储持续时间以及如何增加开始日期,所以我对时区等没有任何问题。

感谢您的帮助!

【问题讨论】:

    标签: php database symfony doctrine-orm


    【解决方案1】:

    一种只用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:

    要回答您的时区问题,最容易(我认为)是使用时间戳!在显示时,时间戳将被转换为时区日期。从 datetime 获取时间戳时,也是一样的,都是按照机器的时区计算的。所以时间戳在时区之间共享^^

    这里是sn-p编辑的:

    // ...
    
    // 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
    

    【讨论】:

    • 这是个好主意,但是如果我添加一些保留时间,例如 4 天,第二天会有时移怎么办?我不认为它会起作用。对吗?
    • 检查我上次的编辑,但这是一个完整的 PHP 解决方案,我不知道它是否适合您,或者您是否想要一个唯一的学说解决方案!
    【解决方案2】:

    除了 Sybio 的回答之外,您还可以在预订期间设置 time 数据类型。然后 Doctrine 将接受 \DateInterval 的实例。

    $reservation
        ->setStartDate(new \DateTime('now'))
        ->setDuration(\DateInterval::createFromDateString('75 minutes'))
    ;
    

    然后在您的控制器中,您可以执行以下操作:

    $em = $this->getDoctrine()->getManager();
    $reservation = $em->getRepository('AcmeMassageParlorBundle:Reservation')->find($id);
    
    // The instance has to be cloned before being modified, to avoid accidentally
    // altering the start time.
    $endDate = clone $reservation->getStartDate();
    $endDate->add($reservation->getDuration());
    
    // To get the date time in ISO format, use
    $endDate->format('Y-m-d H:i:s');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-21
      • 1970-01-01
      • 1970-01-01
      • 2011-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多