开发者

get initial and final dates to INSERT 1 row per month into mysql

I am trying to get the difference开发者_如何转开发 between 2 dates, and get the last day of each month to insert data do the database. So if there is 3 months on this data range, it will insert 3 row to the database (one for each month).

What I have now is:

$jj = '2007-12-31';
$kk = '2009-12-31';


$begin = new DateTime($jj);
$end = new DateTime($kk);

$interval = DateInterval::createFromDateString('last thursday of next month');
$period = new DatePeriod($begin, $interval, $end, DatePeriod::EXCLUDE_START_DATE);

foreach ( $period as $dt ) {
  echo $dt->format( "Y-m-t\n" );

}

This will output the last day of each month of this date range, but now I need to add a new row on a mysql table for each month.

If it helps to understand better my question, this is to save monthly payments, but there will be payments for 3 months, 6 months, 1 year, .... and all the payments will be stored in a monthly basis.

Thank you in advance!


If I understand correctly ... would you not do something like:

   $insertSQL = "INSERT INTO <table> (%d, '%s', ...);"; 

    foreach ( $period as $dt ) {
        $payDate = $dt->format( "Y-m-t\n" );
        mysql_query(sprintf($insertSQL, $custID, $payDate, ...);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜