Reputation: 16793
I need to populate a table with a row for each day over the past 5 years?
E.g
1. 04/11/2012
2. 03/11/2012
3. 02/11/2012
4. 01/11/2012
5. 31/10/2012
6. etc
This will just be a php script that I run once and inserts all of the records.
The reason for doing this is for performance testing my other code with enough data in the database.
Upvotes: 0
Views: 115
Reputation: 11908
$now = new DateTime(); // Now
$date = $new DateTime();
$date->sub(new DateInterval('P5Y')); // 5 years ago
while ($date->diff($now))->invert != 1)
{
// insert $date into database
// I'll leave that to you, as you were looking for a way to loop over dates
$date->add(new DateInterval("P1D"));
}
Upvotes: 1
Reputation: 2678
This will give you the date-format you want:
date('d/m/Y', strtotime($i." days"));
$i is the number of days past/ahead of this date. So if $i is -1 that means one day ago. I guess you could make a loop of it, decreasing/increasing $i.
Upvotes: 1
Reputation: 6078
Like this?
$date = "2007-01-01";
while ($date != "2013-01-01") {
//do your query... not going to do any escaping for brevity
mysql_query("INSERT INTO TABLENAME (SomeDateColumn) VALUES ('$date')");
$date = date("Y-m-d", strtotime($date) + 86400);
}
Upvotes: 1