Php check if date is between dates

Check if the current date is between two dates + mysql select query

I want to check if today’s date is in between dateStart and dateEnd . The following is my query for this :

$todaysDate="2012-26-11"; $db = Zend_Registry::get("db"); $result = $db->fetchAll("SELECT * FROM `table` WHERE active=0 AND between dateStart and dateEnd"); return $result; 

8 Answers 8

Try this :: This will solve your problem

SELECT * FROM `table` WHERE active=0 AND CURDATE() between dateStart and dateEnd 

MySQL uses YYYY-MM-DD by default:

And you need single quotes around it in the query.

$sql = mysql_query("SELECT * FROM register WHERE CURDATE() between reg_start_date and reg_end_date") or die(mysql_error()); 

You needed to put single quotes around the changed date:

"SELECT * FROM `table` WHERE active=0 AND '' between dateStart and dateEnd" 
 $date=date("m/d/Y"); $data=$this->Comman_model->select_manuvl('select * from company_news_details where userid='.$_SESSION['user_key'].' AND "'.$date.'" between `rdate` AND `edate`'); 

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you’ve made.

Источник

PHP function to check time between the given range?

I am using PHP’s Date functions in my project and want to check weather a given date-time lies between the given range of date-time.i.e for example if the current date-time is 2010-11-16 12:25:00 PM and want to check if the time is between 2010-11-16 09:00:00 AM to 06:00:00 PM. In the above case its true and if the time is not in between the range it should return false. Is there any inbuild PHP function to check this or we will have to write a new function??

Читайте также:  Python global interpreter lock gil

2 Answers 2

Simply use strtotime to convert the two times into unix timestamps:

A sample function could look like:

function dateIsBetween($from, $to, $date = 'now') < $date = is_int($date) ? $date : strtotime($date); // convert non timestamps $from = is_int($from) ? $from : strtotime($from); // .. $to = is_int($to) ? $to : strtotime($to); // .. return ($date >$from) && ($date < $to); // extra parens for clarity >

The function to check if date/time is within the range:

function check_date_is_within_range($start_date, $end_date, $todays_date) < $start_timestamp = strtotime($start_date); $end_timestamp = strtotime($end_date); $today_timestamp = strtotime($todays_date); return (($today_timestamp >= $start_timestamp) && ($today_timestamp

Call function with parameters start date/time, end date/time, today’s date/time. Below parameters gets function to check if today’s date/time is between 10am on the 26th of June 2012 and noon on that same day.

if(check_date_is_within_range('2012-06-26 10:00:00', '2012-06-26 12:00:00', date("Y-m-d G:i:s"))) < echo 'In range'; >else

Источник

how to check two dates between from-date and to-date

I have two dates from-date & to-date . I have to compare them from existing dates shown below, whether any of the day fall between them or not using php?
i can do for single date checking ,but i am confuse for the two date checking. Example: i have to check these dates:-> from= 15 March 2013 & 15 April 2013 between the following dates whether any days falls in between these two date or not.

following data from db table

 # from date to-date ----------------------------------------- 1 01 April 2013 30 April 2013 //here we will find as falling 2 01 May 2013 15 May 2013 3 01 June 2013 20 June 2013 

Currently,in my mind not even a single logic is coming to try. Please give me any logic or suggestions regarding this issue..

Yes Exactly. I have mention the same in my question. Its like i have to set my Amount Budget between two dates, if I have already set budget from newly type from-date & to-date. Then i have to show error message

@mpm first time hearing about php Datetime class . i am checking it now. This is my first project in php

2 Answers 2

The simplest way to compare dates is to convert them to a unix timestamp

Because the unix timestamp is an integer, you can simply use relational operators to compare them.

// set some example data $referenceDate = '01 April 2013'; $fromDate = '01 January 2013'; $toDate = '01 June 2013'; // convert dates to timestamps (strings to integers) $referenceTimestamp = strtotime( $referenceDate ); $fromTimestamp = strtotime( $fromDate ); $toTimestamp = strtotime( $toDate ); // isBetween is Boolean TRUE if reference date is greater or equal fromDate and smaller or equal toDate $isBetween = $referenceTimestamp >= $fromTimestamp and $referenceTimestamp  

To actually answer your question:

You have two ranges you need to test for overlap, this question has been answered here What's the most efficient way to test two integer ranges for overlap?

// our two ranges overlap if the following conditions are met $dateRangeOverlaps = $referenceFromTimestamp  

Источник

php if statement to check if current datetime is between 2 datetime columns

PostQopen and PostQClose are datetime columns. I need a php if statement to check if the current datetime is between the PostQopen and PostQClose columns.

$now = date('Y-m-d H:i:s'); if($rows['PostQopen'] >= '$now' && $rows['PostQClose'] < '$now' )< echo "TRUE"; >else < echo "FALSE"; >; 

You don't need single quotes around the '$now' - try removing it and seeing if that fixes the problem.

5 Answers 5

$now = new DateTime("now"); $PostQopen = new DateTime($rows['PostQopen']); $PostQClose = new DateTime($rows['PostQClose']); if($PostQopen >= $now && $PostQClose <=$now)< // do ur stuff >

THIS WORKED FOR ME. $now = date('Y-m-d H:i:s'); $PostQopen = $rows['PostQopen']; $PostQClose = $rows['PostQClose']; if($now >= $PostQopen && $now < $PostQClose)< echo "TRUE"; >;

well u should avoid using strtotime() and use DateTime instead since strtotime() has many limitations and does not work in many cases.

$now = date('Y-m-d H:i:s'); if(strtotime($rows['PostQopen']) >= strtotime('$now') && strtotime($rows['PostQClose']) < strtotime('$now') )< echo "TRUE"; >else < echo "FALSE"; >; 

Check if this works, strtotime converts given time into millisecond format, It should work

Remove single quotes from $now and use strtotime() . Still without strtotime() , the code will work since you can compare time if it is in datetime format .

if(strtotime($rows['PostQopen']) >= strtotime($now) && strtotime($rows['PostQClose']) < strtotime($now) ) < echo "TRUE"; >else

Function to find out between using DateTime is here:

function dateIsBetween($from, $to, $date="now") < $date = new \DateTime($date); $from= new \DateTime($from); $to = new \DateTime($to); if ($date >= $from && $date return false; > 
dateIsBetween('2016-02-24', '2016-02-26', '2016-02-25'); //would return true dateIsBetween('2016-02-24', '2016-02-26', 'now'); //would return true if the current date would be between specified to, from dateIsBetween('2016-02-24', '2016-02-26'); //would return true if the current date would be between specified to, from 

Источник

PHP - How can I check if a date lies between two existing dates?

I am trying to make a booking page that takes an input of check in and check out date from the user as well as the room number. This is my current code; its current function is to get all bookings for a selected room number and check all its date ranges to see one of the current bookings overlaps the user input dates. if it overlaps then echo an error otherwise do nothing. :

$sql18 = "SELECT rid, checkin, checkout FROM bookings WHERE rid = $rNumber"; //$rNumber is room number obtained from a dropdown list $results18 = mysqli_query($conn, $sql18); while ($row = mysqli_fetch_array($results18)) < if (('$cInDate' >= $row["checkin"] && '$cInDate' < $row["checkout"]) || //$cInDate and $cOutDate are user inputs ('$cOutDate' >$row["checkin"] && '$cOutDate' > 

i tested the if statement to trigger the error with no luck. i am still getting past the if statement with no errors

For starts you should remove the '' in $cOutDate and $cInDate, if you use it like that '$cInDate' you are comparing it as a string

WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST or $_GET data directly into a query, it can be very harmful if someone seeks to exploit your mistake.

Источник

Оцените статью