PHP - Populate select options with 15 min time intervals - php

Hi I am creating a website for a restaurant delivery service. Basically when the customer is checking out, he/she can choose when they want the food to be delivered. I want the select box to contain time intervals of 15 mins ranging from the current time until the close time. The restaurant is open for deliveries between 11:00 to 23:00. The first option I want is to be "As soon as possible", and then the next option is an hour later (rounded to nearest 15mins), and then 15 mins each time. So basically something like this:
(Suppose current time is 13:55)
As soon as possible
15:00
15:15
15:30
15:45
...and so on until close time (23:00)
If the restaurant is closed (after 23:00) then I just want the select box to have an option that says "CLOSED".
Here is what I have tried so far but it is not working:
<select>
<?php
$timenow = date("H:i");
if($timenow >"23:00" || $timenow < "11:00"){
echo '<option value="Closed">CLOSED</option>';
echo "</select>";
}
else{
$deliverytime = date("H:i", strtotime('+15 minutes', $timenow));
echo '<option value="asap">As soon as possible</option>';
while($deliverytime < "23:00" && $deliverytime > "11:00"){
echo '<option value="'. $deliverytime.'">' . $deliverytime . '</option>';
$deliverytime = date("H:i", strtotime('+15 minutes', $deliverytime));
}
echo "</select>";
}
?>

strtotime('+15 minutes', $timenow) is not correct. The second argument should be a timestamp, not a string. You want something like strtotime('+15 minutes', time()) or just leave off the second argument (current time is the default).
A better approach is to always work with the timestamps until you output. That makes rounding and comparisons much easier.
<select>
<?php
$timenow = time();
$opentime = strtotime('11:00');
$closetime = strtotime('23:00');
if($timenow > $closetime || $timenow <= $opentime){
echo '<option value="Closed">CLOSED</option>';
echo "</select>";
}
else{
// you said you wanted the time to start in 1 hour, but had +15 minutes...
$deliverytime = strtotime('+1 hour', $timenow);
// round to next 15 minutes (15 * 60 seconds)
$deliverytime = ceil($deliverytime / (15*60)) * (15*60);
echo '<option value="asap">As soon as possible</option>';
while($deliverytime <= $closetime && $deliverytime >= $opentime) {
echo '<option value="'. date('H:i', $deliverytime) .'">' . date('H:i', $deliverytime) . '</option>'."\n";
$deliverytime = strtotime('+15 minutes', $deliverytime);
}
echo "</select>";
}

I can't provide a complete solution but can point you in the right direction.
This code will handle the 15 minute interval parts for you:
$start = new DateTime();
$end = new DateTime('11PM');
$interval = new DateInterval('PT15M');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
echo $dt->format("l Y-m-d") . PHP_EOL;
}
The accepted answer in this Stack Overflow question shows how to get to the nearest 15 minute interval: PHP DateTime round up to nearest 10 minutes
Combine the two and you should have a complete solution to your problem.

Related

how to check if time is more than 30minute?

I have saved a time. I would like to check whether the saved time is 30 minutes greater than the current time. You can see in the code what I have done so far but it doesn't work.
I would need some help fixing that code.
$current = "08:05";
$to_check = date('h:i');
if($to_check > $current + strtotime('30 minute')){
echo "greater";
}
else {
echo "not greater";
}
First, your $current isn't the current time, $to_check is, so your variable names are misleading.
That being said, store your "08:05" as a Unix timestamp then see if the difference between the two is greater than 30 * 60.
$seconds = 1800; // 30 mins
$time1 = strtotime($db_time) + $seconds;
if(time() >= $time1)
echo "Time's up!"
else
echo "time has not expired";
This should work for you:
<?php
$current = date('H:i');
$to_check = date('H:i', strtotime('+30 minutes'));
$to_check = date('H:i', strtotime($record['date'])); //If value is from DB
if($to_check > $current){
echo "greater";
}
else {
echo "not greater";
}
Save time using time function this would give you time in seconds from 1970 so for example current time is 1501137539 and after 30 minutes it would be (1501137539 + 30*60) so you would just need to check if difference b/w current time and stored time is greater that 30*60 then half an hour is over.
Try this snippet:
function x_seconds($to_check = 'YYYY-mm-dd H:i:s') {
$to_check = strtotime($to_check);
$current = time() + 1800; //We add 1800 seconds because it equals to 30 minutes
return ($current>=$to_check) ? true : false;
}
Or go pro, and have a custom "seconds since", just because you can?
function x_seconds($to_check = 'YYYY-mm-dd H:i:s', $seconds = 1800) {
$to_check = strtotime($to_check);
$current = time() + $seconds; //We add x seconds
return ($current>=$to_check) ? true : false;
}
Example usage:
$date = date('Y-m-d H:i:s', '2017-07-27 05:00');
if(x_seconds($date)):
print "Is within 30 minutes.";
else:
print "IS NOT within 30 minutes";
endif;
Try this to solve the issue
$current = strtotime('12:00:00');
$to_check = strtotime(date('H:i:s'));
$newtime = round(( $to_check - $current ) /60 );
if($newtime > 30){
echo "greater";
}else {
echo "not greater";
}

PHP date increment every x minutes and loop until x times

I'm new in php.
I want to ask how to make increment loop date every x minus or x hours and repeat until x times.
Example :
I have time : 2016-03-22T23:00:00
I want to increment that time every 30 minutes
And repet until 6 times.
and output will be :
2016-03-22T23:00:00
2016-03-22T23:30:00
2016-03-23T00:00:00
2016-03-23T00:30:00
2016-03-23T01:00:00
2016-03-23T01:30:00
My former code form other question in stackoverflow :
<?php
$startdate=strtotime("next Tuesday");
$enddate=strtotime("+1 weeks",$startdate); //16 weeks from the starting date
$currentdate=$startdate;
echo "<ol>";
while ($currentdate < $enddate): //loop through the dates
echo "<li>",date('Y-m-d', $currentdate),"T";
echo date('H:i:s', $currentdate);
echo "</li>";
$currentdate = strtotime("+30 minutes", $currentdate); //increment the current date
endwhile; // calculate date range
echo "<ol>";?>
On that code the loop stop until desire date.
My problem is how to make increment in time until desire times..., for example 5 , 10, 10, 500 times loop?
How to code that?
PHP's DateTime feature has the perfect option to do what you want:
// Set begin date
$begin = new DateTime('2016-03-17 23:00:00');
// Set end date
$end = new DateTime('2016-03-18 23:00:00');
// Set interval
$interval = new DateInterval('PT30M');
// Create daterange
$daterange = new DatePeriod($begin, $interval ,$end);
// Loop through range
foreach($daterange as $date){
// Output date and time
echo $date->format("Y-m-dTH:i:s") . "<br>";
}
Just use for-loop for this task:
<?php
$desiredtimes = 500; // desired times
$startdate=strtotime("next Tuesday");
$currentdate=$startdate;
echo "<ol>";
for ($i = 0; $i < $desiredtimes; $i++){ //loop desired times
echo "<li>",date('Y-m-d', $currentdate),"T";
echo date('H:i:s', $currentdate);
echo "</li>";
$currentdate = strtotime("+30 minutes", $currentdate); //increment the current date
} // calculate date range
echo "<ol>";?>
Use a DateTime object, then setup a loop that will iterate exactly 6 times.
After printing the current datetime, increase the datetime object by 30 minutes.
Code: (Demo)
$dt = new DateTime("2016-03-22T23:00:00");
for ($i = 0; $i < 6; ++$i, $dt->modify('+30 minutes')) {
echo $dt->format("Y-m-d\TH:i:s") . "\n";
}
Output:
2016-03-22T23:00:00
2016-03-22T23:30:00
2016-03-23T00:00:00
2016-03-23T00:30:00
2016-03-23T01:00:00
2016-03-23T01:30:00
This was inspired by a more complex process that I scripted for CodeReview.

populate time interval with 30 mintutes between 2 dates

I have 2 time strings for example OPEN = 11:00 and closed is 02:45
now the 02:00 is the next day so i to the following:
//open = string 11:00
//closed= string 02:45
$open = \DateTime::createFromFormat('H:i', $open);
$closed = \DateTime::createFromFormat('H:i', $closed);
if ($open > $closed) $closed->modify('+1 day');
now i have 2 proper datetime formats. Now i want to set a time interval of 30 minutes from the open time to the close time. How can i do this? i have read i can add like this
->add(new DateInterval('PT30M') b
ut it will add till the end of the day.. but in this case its open till the next day, so i want it to populate till 2:45AM
how can i do this?
You where on the right track. Here is how to do it using DateInterval('PT30M').
$strOpen = '11:00';
$strClose = '02:45';
$open = DateTime::createFromFormat('H:i', $strOpen);
$closed = DateTime::createFromFormat('H:i', $strClose);
if ($open > $closed) $closed->modify('+1 day');
// I display not only the time, but the day as well to show that it is
// incrementing to the next day.
echo 'open: ' . $open->format('D H:i') . "<br />\n";
while ($open < $closed) {
$open->add(new DateInterval('PT30M'));
// because incrementing on the half hour and our finish is on the 15 min,
// the last is $open < $close in the while statement will be true but
// this loop will generate a time after $closed so we do another check
// now to eliminate that issue
if ($open < $closed) {
echo '+30min: ' . $open->format('D H:i') . "<br />\n";
}
}
echo 'closed: ' . $closed->format('D H:i') . "<br />\n";

Compare h:i:s to see which hour is greater including midnight - PHP

Okay so I have a function that compares the hours from 00:00:00 - 24:00:00 to tell the closing time. Now the issue I have is, lets say the store opens at 8AM and closes at midnight, well 00:00:00is going to be less than 23:00:00. How would I go about making sure that it recognizes that midnight is greater?
Here is what I am doing:
if((strtotime($openTime) < time()) && (strtotime($closeTime) > time()))
{
$response['data_retrieved']['store'][$i]['store_settings']['open'] = 1;// open
}
else
{
$response['data_retrieved']['store'][$i]['store_settings']['open'] = 0; //close
}
Suggestions?
Since there is no 24:00:00, you'll need the full date string Y-m-d H:i:s to do an accurate comparison.
Even just adding in the date won't be enough, as it will break when looking at the last day of the month ('31 08:00:00' < '1 00:00:00').
PHP has a very good DateTime support. Its better to use that instead of reinventing things. Also like #mopo922 suggested, you'll need the full Y-m-d H:i:s
The answer #mopo922 provided above is the most foolproof and most correct answer.
Another method would be to "add" 24 hours to the $closeTime if $startTime > $closeTime. It's quite dodgy but still solves the problem.
<?php
date_default_timezone_set('Australia/Sydney');
$openTime = strtotime('08:00');
$currentTime = strtotime('23:00');
$closeTime = strtotime('10:00');
echo 'open: ' .$openTime . '<br>' .
'time(): ' . $currentTime . '<br>' .
'close: ' . $closeTime . '<br>';
// 1 day = 86400 seconds
if ($closeTime < $openTime) {
$closeTime += 86400;
}
if($openTime < $currentTime && $closeTime > $currentTime)
{
echo 'open';
}
else
{
echo 'closed';
}
?>

select menu with current date

How do you create a select with the option of 2 days ahead from today picked as the default option (i.e. a 48 hour window) in PHP? This is the code I'm using so far but it doesn't work unfortunately!
<?php
$weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$days = range (1, 31);
$currentDay = date('F');
echo "<select name='weekday'>";
foreach ($days as $value) {
$default = ($value == $currentDay)?'selected="selected"':'';
echo '<option '.$default.' value="'.$value.'">'.$value."</option>\n";
}
echo '</select> ';
?>
I'm confused as to what your code does.
As far as I can tell $weekday is not used after being instantiated, and you are setting $currentDay to the text representation of the current month (e.g. September).
But if you want to get the day of month of the day 48 hours from now:
$two_days_ahead = date('j', strtotime('+ 48 hours'));

Categories