1

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?

Community
  • 1
  • 1
Tan Jia Ming
  • 435
  • 1
  • 6
  • 22
  • 2
    Since you're aware of PHP5.3 but still need an answer for 5.2, I guess you're using a hosting provider that is refusing to upgrade. It's important to note that PHP 5.2 has been completely unsupported for two years now, and [5.2.6 is nearly 5 years old](http://uk.php.net/releases/index.php). There are known security issues even in the latest 5.2 that have not and will never be patched. If your host that hasn't kept up with security patches for five years, you should seriously be switching to a better host. – SDC Dec 19 '12 at 13:13
  • It is actually...my company's development server. Your answer surprised me, and I will see what can I do with them. – Tan Jia Ming Dec 19 '12 at 13:37

2 Answers2

3

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.

Aaron W.
  • 9,254
  • 2
  • 34
  • 45
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']));
ozahorulia
  • 9,798
  • 8
  • 48
  • 72