我每天都有一个包含表格的Mysql数据库,它是这样的:my_table_20130310
,my_table_20130311
...
我想对所有表格进行选择,让我们说'2013/02/02'和'2013/03/28'
有这个愚蠢的解决方案,即将给定日期转换为:
'2013/02/02' => 20130202
'2013/03/28' => 20130328
然后将转换为(int)然后执行循环:
$dbStartDate = str_replace('/', '', substr($startDate, 0, 10));
$dbEndDate = str_replace('/', '', substr($endDate, 0, 10));
for ($tbDate = $dbStartDate; $tbDate <= $dbEndDate ; $tbDate++) {
$res = 'SELECT * FROM my_table_' . $tbDate ;
...
...
}
但这不是一个解决方案,因为它会尝试解析20130202和20130328(20130299,20130245 ......)之间的所有数字。
一个想法?
Thx guys
答案 0 :(得分:2)
在伪代码中:
DateTime startDate = new DateTime('2013-02-02');
DateTime endDate = new DateTime('2013-03-08');
for(DateTime d = startDate; d <= endDate; d = d.Add(new DateInterval('P1D')))
{
res = 'SELECT * FROM my_table_' . d.format('YYYYmmdd');
//...
}
答案 1 :(得分:1)
试试这个:
$dbStartDate = new DateTime('2013/02/02');
$dbEndDate = new DateTime('2013/03/28');
$diff = $dbEndDate->diff($dbStartDate)->days; // get the difference in N days
// just a loop :p
$currentDate = $dbStartDate;
for($i=0;$i<=$diff;$i++){
$query = 'SELECT * FROM my_table_' . $currentDate->format('Ymd');
date_add($currentDate, date_interval_create_from_date_string('1 day'));
// echo $query."<br/>"; // for testing purposes
}