php mysql compare dates and match with user input dates -


this question has answer here:

i doing booking page house renting.

i have datepicker users submits unix datetime: arrival , departure date.
on mysql have house different seasons "season_start" date (yyyy-mm-dd), "season_end" date & "price per day".

$seasons_select="select id,season_start,season_end,daily_price ".t_item_seasons." id_item=".id_item." order season_start "; $res_seasons=mysql_query($seasons_select) or die("error getting states"); $arrival_date = date('d-m-y', $arrival_date); $departure_date = date('d-m-y', $departure_date);  while ($seasons_row=mysql_fetch_assoc($res_seasons)){   $start_date = date('d-m-y', strtotime($seasons_row["season_start"]));   $end_date = date('d-m-y', strtotime($seasons_row["season_end"]));   $current_date = $start_date;   while($current_date != $end_date){    $current_date = date("d-m-y", strtotime("+1 day", strtotime($current_date)));    $match_date = $arrival_date;    while($match_date != $departure_date){     $match_date = date("d-m-y", strtotime("+1 day", strtotime($match_date)));     if ($current_date==$match_date){       echo $current_date.' 1 of days! <br />';     }           }       }   } 

what best way match if date inside user arrival/departure period inside 1 or severall seasons?

my code kind of works...
answer want after "fatal error: maximum execution time of 30 seconds exceeded in"

what best way match if date inside user arrival/departure period days inside 1 or severall seasons?

well, yours looks kinda terrible :-)

i say, let mysql work – select records want in first place.

to check if single date within “season”, have check if greater-equal season begin , lesser-equal season end.

to seasons day of arrival/departure period falls into, have check 4 cases:

(| season start , end, , a select arrival , d departure date.)

s: -------------------|-----------------|------------------ 1. ---------------a--------------------------d------------- 2. ---------------a-----------------d---------------------- 3. -----------------------a---------d---------------------- 4. -----------------------a------------------d------------- 
  1. season falls selected period completely.

  2. arrival before season start, departure before end.

  3. arrival after season start, departure before end.

  4. arrival after season start, , departure after season end.

(before , after meant including, before/on , on/after.)


put inside query, this, 2013-05-04 arrival , 2013-07-04 departure date:

select id, season_start, season_end, daily_price table id_item = 1234 , (    ('2013-05-04' <= season_start , '2013-07-04' >= season_end) or    ('2013-05-04' <= season_start , '2013-07-04' <= season_end) or    ('2013-05-04' >= season_start , '2013-07-04' <= season_end) or    ('2013-05-04' >= season_start , '2013-07-04' >= season_end) ) order season_start 

since said receive user input unix timestamps, can either format php’s date() before inserting them query, or use mysql’s from_unixtime(123456789, '%y-%m-%d') in places instead.

and should create index (if not in place) on columns season_start , season_end (one index each), better performance.


edit: pointed out in this answer salman referred in comments, 2 conditions (and little more creativity in applying boolean logic had) enough check, clause can simplified as

where id_item = 1234 ,   ('2013-05-04' <= season_end) , ('2013-07-04' >= season_start) 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -