[英]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.