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 8189137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:10:16+00:00 2026-06-07T03:10:16+00:00

i have table with two fields competition {stateTime, endTime} when i insert to that

  • 0

i have table with two fields

competition {stateTime, endTime}

when i insert to that table i want to ensure that the value i want to insert it is not on the period of any row in that table
i type this function (PDO Database)

function isCompetitionInAnotherCompetition($startTime, $endTime) {
        $query = "SELECT * FROM competition";
        $sth = $this->db->prepare($query);
        $sth->execute(array());
        while ($row = $sth->fetch()) {
            if ($startTime >= $row['startTime'] && $startTime <= $row['endTime'])
                return true;
            if ($endTime >= $row['startTime'] && $endTime <= $row['endTime'])
                return true;
        }
        return false;
    }

but doesn’t work good,
every date in database is yyyy-mm-dd, example 2012-01-15

  • 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-07T03:10:18+00:00Added an answer on June 7, 2026 at 3:10 am

    May i suggest that rather then writing a function that tests existence and returns a bool that you return the records, this way you can later test whether none were returned but if there was you can the use them if needed. As Alvin Wong suggests you can use BETWEEN in your sql so you get something like this.

    function getCompetitionsBetween($startTime, $endTime) {     
    
        $startTime = date("Y-m-d", $startTime);
        $endTome = date("Y-m-d", $startTime);
    
        $query = "SELECT * FROM competition 
                   WHERE start_time BETWEEN ? AND ? 
                   OR end_time BETWEEN ? AND ?";
    
        $sth = $this->db->prepare( $query );
    
        $sth->execute( array($startTime, $endTime, $startTime, $endTime) );
    
        return $sth->fetchAll();
    }
    

    and later/somewhere else

    $competitions = getCompetitionsBetween($startTime, $endTime);
    
    if (empty(competitions)) {
        $this->save();
    } else {
        echo ('sorry the following competitions conflict with these dates');
        foreach($competitions as $k => $v) {
            echo ($k . ':' . $v);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a table with a datestamp and two fields that I want
I have one table that has two fields - ID1 and ID2 ID1 can
I have a table, myTable that has two fields in it ID and patientID
following on from my other post... I have a table that uses two fields
I have a table that have two fields. table test { fname char(20), id
I have a mysql table that has two fields that store the same type
I have a table with two fields, named credithour and gradepoint . I want
I have a table with two fields; firstname & lastname I want to select
I have a database table that has two fields , date and name. I
I have a table and want to insert the current values of records that

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.