0

わかりました、ここではコーディングよりも物流上の質問がありますが、コード例/ソリューションは両方に答える可能性があるので、ここに行きます...

PHP で start_date と end_date (完全に m/d/Y 形式) で渡すフォームがあり、その範囲に該当する mysql データベースのすべてのエントリのテーブルを出力する必要がある場合 (m1/d1/ からY1 から m2/d2/Y2) を月ごとにグループ化する場合、どのようにすればエレガントな方法でそれを行うことができますか?

たとえば、start_date = 5/20/2012、end_date = 7/31/2012 を渡す場合、次の形式で結果を表示するには、結果のテーブルが必要です。

May 2012  |  June 2012  |  July 2012
....      |  ....       |  .....

2012 年 5 月の最初の列には、次のような mysql クエリの結果が表示されます。

SELECT ... WHERE signup_date >= 5/20/2012 AND signup_date <= 5/31/2012

2012 年 6 月も同様に次のように表示されます。

SELECT ... WHERE signup_date >= 6/1/2012 AND signup_date <= 6/30/2012

そのため、開始日と終了日を解析して、月の開始日から月末まで適切に配置された日付の配列に変換する方法を探しています。end_date が数か月後の場合は、他のすべてをカバーします。その間の月全体 (その月の 1 日から最後まで) を使用して、for/while ループでそれらを循環させることができますか? 次のようなものです:

[0]['開始'] =​​> '2012 年 5 月 20 日'

[0]['終了'] => '2012 年 5 月 31 日'

[1]['開始'] =​​> '2012 年 6 月 1 日'

[1]['終了'] => '2012 年 6 月 30 日'

[2]['開始'] =​​> '7/1/2012'

[2]['終了'] => '2012 年 7 月 31 日'

何か案は?

4

3 に答える 3

0

mktime()でこれを達成する必要があります。以下サンプルコード。

<?php
$startDate = '05/20/2012';
$endDate = '07/31/2012';


$curMonth =  date("m", strtotime($endDate)); //End Month
$curYear = date("Y", strtotime($endDate)); //End Year

$months = array();
$months[] = array('start' => "$curMonth/01/$curYear", 'end' => $endDate);

while ("$curMonth/01/$curYear" > $startDate) {
        $monthEnd =  date("m/d/Y", mktime(0, 0, 0, $curMonth, 0, $curYear));
        $curMonth = date('m', strtotime($monthEnd));
        $curYear = date('Y', strtotime($monthEnd));
        $monthStart = ($curMonth/01/$curYear > $startDate) ? $startDate : "$curMonth/01/$curYear";
        $months[] = array('start' => $monthStart, 'end' => $monthEnd);
}

print_r(($months));
?>
于 2012-11-02T15:27:00.900 に答える
0

これを試して:

$date = '5/20/2012';
$dateStart = date("m/01/y", strtotime($date));

$start = date("Y-m-d", strtotime($dateStart));
$end = date("Y-m-d", strtotime($dateStart. '+ 1 month - 1 day') );

echo $start;
echo $end;
于 2012-11-02T04:45:18.650 に答える
-1

mysql db の日付形式については、最初に私のコメントを参照してください。

疑似言語で少し書かれています

     $sql="SELECT * FROM ... WHERE `signup_date`>='2012-5-20' AND `signup_date`<'2012-5-31' ORDER BY  `signup_date`";


        $result=mysql_query($sql);

    $month_year=array() //3-dimensional array $month_year[year][month][primary_key];

    $month_index=-1;
    while($row=mysql_fetch_assoc($result)){

    // find $month_of_the_date AND $year_of_the_date

    //find different months ,corresponding years, and db primary key and keep track in the $month_year array




    }

    for( all the years in the array ){

    for(all the corresponding months of the year  in the array){

//**EDIT:** you MIGHT need other loops and arrays to collect distinct years and corresponding months together

    // show the records with the primary keys corresponding to that month and year



    }




    }
于 2012-11-02T04:45:44.073 に答える