hyster Posted December 14, 2010 Share Posted December 14, 2010 i have a form that inputs into a mysql database. 2 of the fields are dates. from and to. ive been trying to search for a way to insert into the database for the days inbetween the dates given and i dont no if to do this in php or the mysql query. $post_a = val1 $post_b = val2 $post_c = val3 $post_d = val4 $post_e = 10/12/2010 $post_f = 15/12/2010 $date = loop of $post_e to $post_f( 10/12/2010 11/12/2010 12/12/2010 13/12/2010 14/12/2010 15/12/2010) $sql="insert into $table (a, b, c, d, date) values ('$post_a', '$post_b', '$post_c', '$post_d', '$date') "; Quote Link to comment Share on other sites More sharing options...
marain Posted December 14, 2010 Share Posted December 14, 2010 why insert into this date?? why not in select? sql = "select * from table where date between time1 and time2" Quote Link to comment Share on other sites More sharing options...
hyster Posted December 14, 2010 Author Share Posted December 14, 2010 i need to put data into the database, not pull from it. i want to do it the "easy" way and not have to add the dates in 1 by 1, hence add to database between date1 and date2. Quote Link to comment Share on other sites More sharing options...
hyster Posted December 14, 2010 Author Share Posted December 14, 2010 hmm so easy i spent 5 hours googling how to do this and came back with nothing usefull. bit like your comment actually. now if somebody can help me out on this and point me in the right direction, your help would be gratefully appreciated. Quote Link to comment Share on other sites More sharing options...
Maq Posted December 14, 2010 Share Posted December 14, 2010 I had to reformat your date because the strtotime function didn't pick up the format. Might have to play around with it: ini_set ("display_errors", "1"); error_reporting(E_ALL); $table = "your_table"; $post_a = "val1"; $post_b = "val2"; $post_c = "val3"; $post_d = "val4"; $post_e = "12/10/2010"; $post_f = "12/15/2010"; while (strtotime($post_e) { $sql="insert into $table (a, b, c, d, date) values ('$post_a', '$post_b', '$post_c', '$post_d', '$post_e') "; echo "$sql\n "; $post_e = date("m/d/Y", strtotime("+1 day", strtotime($post_e))); } ?> Quote Link to comment Share on other sites More sharing options...
hyster Posted December 14, 2010 Author Share Posted December 14, 2010 thanks MAQ. ill have a play with it. Quote Link to comment Share on other sites More sharing options...
marain Posted December 14, 2010 Share Posted December 14, 2010 $oneday = strtotime("1 days"); $date = strval($post_e); for(i=1;i<0;i++){ $post_e = $post_e + $oneday; if(abs($post_e - strtotime($post_f))/86400>0){ break; }else{ $date = $date + "," + strval($post_e); } } echo $date; Quote Link to comment Share on other sites More sharing options...
Maq Posted December 14, 2010 Share Posted December 14, 2010 That code is riddled with fatal errors, lacks formatting, and doesn't make any sense to me. Quote Link to comment Share on other sites More sharing options...
marain Posted December 14, 2010 Share Posted December 14, 2010 I dont know this answer if is your mean. im chinese . i understand your mean; Quote Link to comment Share on other sites More sharing options...
Maq Posted December 14, 2010 Share Posted December 14, 2010 No more food for you. Quote Link to comment Share on other sites More sharing options...
marain Posted December 14, 2010 Share Posted December 14, 2010 $oneday = strtotime("1 days"); $date = strval($post_e); while(abs( strtotime($post_e) - strtotime($post_f) ) / 86400 <= 1) { $post_e = strtotime($post_e) + $oneday; $date = $date + "," + strval(date('d/m/Y'),$post_e); } echo $date; maybe your question is wrong Quote Link to comment Share on other sites More sharing options...
Rifts Posted December 14, 2010 Share Posted December 14, 2010 I cant spot laughing while reading the replies to this post Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.