Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8059937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:49:39+00:00 2026-06-05T09:49:39+00:00

I have this function that gives me a set of options in a select

  • 0

I have this function that gives me a set of options in a select input.
The options give me times with 5 minute interval.
The problem is when the time is like 23:45, the options will start from 00:10 and loops based on the $j variable.

This is what I want to do in words:
Give me a list of options in 5 minutes interval from $open_time till $close_time.
If the current Time ($timeNow) is greater than the $open_time, set the $open_time to the $timeNow to be shown as first option.
Do this loop only until the $close_time.

I hope this is clear.
Appreciate your help 🙂

Here is the code:

function selectTimesofDay(){
    $output = "";
    $now = date('G:i ', time()); // time now
    $timeNow = strtotime($now); // strtotime now
    $next_five = ceil($timeNow / 300) * 300; // get next 5 minute
    // time now rounded to next 10 minute
    $round5minNow = date('G:i', strtotime('+15 minutes',$next_five)); 
    $open_time = strtotime('17:00');
    $close_time = strtotime('23:59');

    // in the middle of working hours, time sets to current
    if($timeNow >= $open_time && $timeNow < $close_time){
        $open_time = strtotime($round5minNow);
     }
    $time_diff = round(($close_time - $open_time)/60) ; 
    if(date('l') == 'Friday'){
        $j = ($time_diff/5)+11; // working hours extended untill 1:00 AM
    } else{
        $j = ($time_diff/5)-1; // working hours untill 12:00 AM
    }

        for($i = 0; $i <= $j; $i++){
            $b = $i*5;  
            $data = date('l')." - ".date("H:i", strtotime('+'.$b.' minutes', $open_time));
            $output .= "<option value=\"{$data}\">";    
            $output .= $data;
            $output .= "</option>";
        }

    return $output;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:49:40+00:00Added an answer on June 5, 2026 at 9:49 am

    What you really need is:

    function selectTimesOfDay() {
        $open_time = strtotime("17:00");
        $close_time = strtotime("23:59");
        $now = time();
        $output = "";
        for( $i=$open_time; $i<$close_time; $i+=300) {
            if( $i < $now) continue;
            $output .= "<option>".date("l - H:i",$i)."</option>";
        }
        return $output;
    }
    

    So what this does is run a loop checking every five-minute interval between opening and closing. Skip it if it is before the curent time, and otherwise add an option.

    It’s much more efficient than what you were trying to do, and probably easier to understand too.

    You can even put this after the loop:

    if( $output == "") return "<option disabled>Sorry, we're closed for today</option>";
    

    Also, notice how I left out the value attribute all the time. That’s because in the absence of a value, the option’s text is used as a value. Thus this solution avoids needless duplication.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function that takes the input of a, runs a calculation and
I have this function that prints the name of all the files in a
I have this function that converts all special chars to uppercase: function uc_latin1($str) {
I have this function that switches the HTML contents from one element on a
So I have this function that forks N number of child processes. However it
I have this function called ProperCase that takes a string, then converts the first
I have this function in jQuery that gets data from a page with POST,
I have this function on my controller (Im using CodeIgniter) that reads the database,
I have this function to edit all fields that come from the form and
I have this function in a Code Igniter model that creates a new video.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.