Jump to content

insert into databae between 2 dates


hyster

Recommended Posts

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') ";

Link to comment
https://forums.phpfreaks.com/topic/221656-insert-into-databae-between-2-dates/
Share on other sites

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.

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)));
}

?>

$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; :P

 

 

 

 

 

maybe your question is wrong  :P

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.