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!
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
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With