简体   繁体   中英

PHP insert script for past amount of days

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.

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM