[英]Getting first day of month in PHP 5.2.6?
我有以下情形:Datepicker,供用戶選擇Year,Month,並用它顯示所選月份的記錄。 使用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'的兩行都失敗了,但是我不理解其他三行的輸出。 我最后附加了“ -01”和“ -31”來表示每月的第一天和最后一天。 有誰能在PHP 5.2.6中工作的更好的解決方案?
您可以放心地假設每個月的第一天為1
。 采用
date('Y-m-t', strtotime($_GET['year_month']));
每月的最后一天。
// 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']));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.