Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony/Doctrine Dateinterval (time duration) how to store it in database

I have a question, I am writing an application that has some kind of reservation for service. The service has a duration time let say for example:

A massage that takes 1 hour and 15 minutes

Then I make a reservation system for this services. While I am doing reservation I need to calculate the end datetime.

So I have a Datetime in my database for "start" and I have no idea how to store the duration. So after reservation I can easily say that is will end in some other datetime.

I hope I was clear enough.

The question is how to store duration in database and how to increase start date with it, so I don't have any problems with timezones etc.

Thanks for help!

like image 840
newicz Avatar asked Jun 02 '13 13:06

newicz


2 Answers

A way to only do with PHP (and not using SQL), the time is manage in second to simplify calculation :

$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();

EDIT 1 :

To answer your timezone issue, the most easier (I think) is to use timestamp ! At display, the timestamp will be converted as a timezone date. When getting timestamp from datetime, it's the same, it is calculated on the timezone of the machine. So timestamp is shared between timezones ^^

Here the snippet edited :

// ...

// Save timestamp instead of datetime
$reservation->setStartTimestamp($startDate->getTimestamp());
$reservation->setEndTimestamp($endDate->getTimestamp());

// ...

EDIT 2 :

To answer your comment, if you have a change of duration, just save the duration in database.

// First save
$reservation->setDuration(4000); // seconds

And when editing the duration :

// 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
like image 56
Sybio Avatar answered Sep 21 '22 13:09

Sybio


In addition to Sybio's answer, you can set a time datatype for the duration of the reservation. Then Doctrine will accept an instance of \DateInterval.

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

Then in your controller, you could do something like this:

$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');
like image 34
Adam Elsodaney Avatar answered Sep 20 '22 13:09

Adam Elsodaney