1

次のシナリオがあります。ユーザーが年、月を選択し、それを使用して選択した月のレコードを表示するための日付ピッカー。選択された値は、Ajax を使用して details_subcontractor.php に渡されます。Datepicker は、jQuery UI DatePicker で受け入れられた回答から月年のみを表示するように変更されました。サンプルコードは次のとおりです。

/**
 *  Datepicker in datepicker.php
 *
 */

$("#year_month").datepicker({"dateFormat":"yy-mm-dd",
    changeMonth: true,
    changeYear: true,
    dateFormat: 'yy-mm',
    showButtonPanel: true,
    onClose: function(dateText, inst) { 
        var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
        var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
        $(this).datepicker('setDate', new Date(year, month, 1));
    }
});

/**
 *  Ajax call
 *
 */

var year_month = $("#year_month").val();
$.ajax({
    url:"details_subcontractor.php",
    data:{
        year_month:year_month
    },
    success:function(data){
        $("#content_report").html(data);
    }
});


/**
 *  details_subcontractor.php
 *
 */

//$_GET['year_month']; //2012-12
$start = date('Y-m-d', strtotime($_GET['year_month']));                     //2012-12-18
$start = date('Y-m-d', strtotime('first day of ' . $_GET['year_month']));   //1970-01-01
$start = date('Y-m-d', strtotime($_GET['year_month'] . ' first day'));      //2012-12-19
$end   = date('Y-m-d', strtotime('last day of ' . $_GET['year_month']));    //1970-01-01
$end   = date('Y-m-d', strtotime($_GET['year_month'] . ' last day'));       //2012-12-17

ただし、インライン コメントが示すように、月の最初の日と最後の日を取得できません。PHP 5.3 で「of」が導入され、「of」を含む 2 行が失敗していることは認識していますが、他の 3 行の出力はわかりません。月の最初と最後の日を示すために「-01」と「-31」を追加することになりました。PHP 5.2.6で動作するより良い解決策はありますか?

4

2 に答える 2

3

毎月1日はであると考えて差し支えありません1。使用する

date('Y-m-t', strtotime($_GET['year_month']));

月の最後の日。

于 2012-12-19T13:09:36.240 に答える
2
// The first day is always 01, so you can hardcode it
$start = date('Y-m-01', strtotime($_GET['year_month']));

// last day you can get by `t` format parameter, which contains total days count for given month.
$end = date('Y-m-t', strtotime($_GET['year_month']));
于 2012-12-19T13:12:06.400 に答える