2012-12-19 56 views
1

我有以下情況:Datepicker供用戶選擇Year,Month,並用它來顯示所選月份的記錄。使用Ajax將選定的值傳遞給details_subcontractor.php。日期選擇器已從jQuery UI DatePicker to show month year only中的已接受答案中修改。下面的示例代碼:在PHP 5.2.6中獲取第一個月的第幾天?

/** 
* 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'的兩行失敗,但我不理解其他三行的輸出。 我最後加上'-01'和'-31'來表示月份的第一天和最後一天。有沒有人有更好的解決方案,在PHP 5.2.6中工作?

+2

由於您知道PHP5.3,但仍然需要5.2的答案,我想你正在使用拒絕升級的託管服務提供商。需要注意的是,PHP 5.2在兩年內完全不受支持,[5.2.6已近5年](http://uk.php.net/releases/index.php)。即使在最新的5.2版本中,也存在已知的安全問題,這些安全問題不會被修補。如果你的主機沒有跟上安全補丁五年,你應該認真切換到一個更好的主機。 – SDC

+0

它實際上是我公司的開發服務器。你的回答讓我感到驚訝,我會看看我能對他們做些什麼。 –

回答

3

您可以放心地假設每個月的第一天是1。使用

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

該月的最後一天。

+0

我們確定每個月的第一天會是第一天? :) – webnoob

+1

不,夥計,有些月份從第3天開始...... – Hast

+0

謝謝,我會回到我的工作場所嘗試它,但'噸'是爲了嗎? 編輯:@Hast回答了我。 –

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']));