gpt4 book ai didi

php - 在 PHP 5.2.6 中获取月份的第一天?

转载 作者:行者123 更新时间:2023-12-01 06:51:45 25 4
gpt4 key购买 nike

我有以下场景:日期选择器供用户选择年、月,并用它来显示所选月份的记录。使用 Ajax 将选定的值传递到details_subcontractor.php。 Datepicker 已根据 jQuery UI DatePicker to show month year only 中接受的答案进行修改。示例代码如下:

/**
* 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

但是,正如内嵌评论所示,我无法获取该月的第一天和最后一天。我知道'of'是在PHP 5.3中引入的,其中包含'of'的两行失败了,但我不明白其他三行的输出。我最终附加了“-01”和“-31”来指示该月的第一天和最后一天。有谁有更好的解决方案,适用于 PHP 5.2.6?

最佳答案

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

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

本月的最后一天。

关于php - 在 PHP 5.2.6 中获取月份的第一天?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13953009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com