doujiabing1228 2012-12-19 13:00
浏览 39
已采纳

在PHP 5.2.6中获得第一天的第一天?

I have following scenario: Datepicker for user to choose Year,Month, and use it to display records for the selected month. The selected value is passed to details_subcontractor.php, using Ajax. Datepicker has been modified from accepted answer in jQuery UI DatePicker to show month year only. Sample code as below:

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

However, as the inline comment shows, I can't get first day and last day of the Month. I'm aware that 'of' is introduced at PHP 5.3, where the 2 lines that contain 'of' are failed, but I don't understand the output of other three. I ended up with appending '-01' and '-31' to indicate first and last day of month. Does anyone have a better solution, that works in PHP 5.2.6?

  • 写回答

2条回答 默认 最新

  • douluoxiao2286 2012-12-19 13:09
    关注

    You can safely assume that the first day of every month is 1. Use

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

    for the last day of the month.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化