I would like to make a booking application one field there is date and time for example: long time of booking and reservations
date of booking : 18-12-2013 15:30:00 And long booking : 6 hours
And the result will be 18-12-2013 21:30:00
what if the date of the message and the old order. hours ended up being 0:30. it's already changed the day or date.
date of booking : 18-12-2013 20:30:00 And long booking : 5 hours
And the result will be 18-12-2013 01:30:00
how to implement it in PHP?
sorry i'm still newbie :-)
my php code
$date_booking = $_POST['datebooking'];
$long_time = $_POST['long']; // its contents were 1, 2, 3, 4, and so on
$result = strtotime($date_booking) + $longtime;
Use strtotime()
to change your date string into an integer (number of seconds since 1970-01-01 ).
Convert the long booking from hours to seconds (*60*60), then do the addition and use date('Y-m-d H:i:s', $endTime)
to convert back to a datetime string.
Note: you will have first to transform your string "DD-MM-YYYY" into "YYYY-MM-DD" to make this works
EDIT with your code:
$date_booking = $_POST['datebooking']; // need to be formatted as "YYYY-MM-DD HH:II:SS" (for example)
$long_time = $_POST['long']; // its contents were 1, 2, 3, 4, and so on
$long_time_sec = $long_time * 3600; // convert hours to seconds
$result = strtotime($date_booking) + $long_time_sec;
echo 'end of booking is '.date('Y-m-d H:i:s', $result);
There is a function strtotime
that accepts dates with different formats and where you can add some amount of time. In your case you can do:
$mydate = strtotime("18-12-2013 15:30:00 + 6 hours");
Then you have a number of seconds from 1970-01-01 (UNIX time) and you can convert to any format you want with date
function:
$returnValue = date('d-m-Y H:i:s', $mydate);
NOTE: I haven't understand if you have "text orders" like date of booking : XXXX And long booking : X hours. If this is the case, you can use the function str_replace
or similar to get rid of the 'date of booking : ' and 'And long booking :' part:
$order = str_replace(array('date of booking : ', 'And long booking :'),
'',
$order);
New PHP code should make use of the DateTime functions. There is nothing intrinsically wrong with strtotime(), but DateTime will handle things like DST changes for you with no bother. In this case your code should be something like this:-
$date_booking = \DateTime::createFromFormat('d-m-Y H:i:s', $_POST['datebooking']);
$long_time = new \DateInterval("PT{$_POST['long']}H");
$date_booking->add($long_time);
echo "Booking ends at " . $date_booking->format('Y-m-d H:i:s');
Read more about formatting DateTime here.