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

  • Home
  • SEARCH
  • 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 8011607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:04:09+00:00 2026-06-04T19:04:09+00:00

I need to check if the current time is in timerange. The most simple

  • 0

I need to check if the current time is in timerange. The most simple case time_end > time_start:

if time(6,0) <= now.time() <= time(12,00): print '1'

But troubles begin when user enters a time range when the end time is smaller than the start time, e.g. “23:00 – 06:00”. A time like ’00:00′ will be in this range. About 5 years ago I wrote this PHP function:

function checkInterval($start, $end)
  {    
    $dt = date("H:i:s");    

    $tstart = explode(":", $start);
    $tend =   explode(":", $end);
    $tnow =   explode(":", $dt);

    if (!$tstart[2])
      $tstart[2] = 0;

    if (!$tend[2])
      $tend[2] = 0;  

    $tstart = $tstart[0]*60*60 + $tstart[1]*60 + $tstart[2];
    $tend   = $tend[0]*60*60   + $tend[1]*60   + $tend[2];
    $tnow   = $tnow[0]*60*60   + $tnow[1]*60   + $tnow[2];

    if ($tend < $tstart)
      {
        if ($tend - $tnow > 0 && $tnow > $tstart)
          return true;
        else if ($tnow - $tstart > 0 && $tnow > $tend)
          return true;
        else if ($tend > $tnow && $tend < $tstart && $tstart > $tnow)
          return true;
        else return false;
      } else
      {
        if ($tstart < $tnow && $tend > $tnow)
          return true;
        else
          return false;
      }

Now I need to do the same thing, but I want to make it good looking. So, what algorithm should I use to determine if the current time ’00:00′ is in reversed range e.g. ['23:00', '01:00']?

  • 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-04T19:04:10+00:00Added an answer on June 4, 2026 at 7:04 pm

    The Python solution is going to be much, much shorter.

    def time_in_range(start, end, x):
        """Return true if x is in the range [start, end]"""
        if start <= end:
            return start <= x <= end
        else:
            return start <= x or x <= end
    

    Use the datetime.time class for start, end, and x.

    >>> import datetime
    >>> start = datetime.time(23, 0, 0)
    >>> end = datetime.time(1, 0, 0)
    >>> time_in_range(start, end, datetime.time(23, 30, 0))
    True
    >>> time_in_range(start, end, datetime.time(12, 30, 0))
    False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a cross-site template object which will check the current time
I need to check in PHP if the current time is before 2pm that
When a function runs I need it to check the current system time and
When a user visits a page I need to check if the current time
In my C# application, I need to check if the current user is a
I am new to JavaScript but need to run a check to make sure
I need to check in some fast way if there is any text nodes
I need to check whether a given date is in the preceding month for
I need to check if a database is totally empty (no tables) using an
I need to check specific positions in an NSArray to see if they have

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.