简体   繁体   English

过去几天的PHP插入脚本

[英]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? 在过去5年中,我每天需要在表格中填充一行吗?

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. 这只是我运行一次并插入所有记录的php脚本。

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. $ i是该日期之前/之后的天数。 So if $i is -1 that means one day ago. 因此,如果$ i为-1,则意味着一天前。 I guess you could make a loop of it, decreasing/increasing $i. 我猜你可以做一个循环,减少/增加$ 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"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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