Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many days does 1 month represent in PHP?

Tags:

php

I see there is strange problem in php with month addition and subtraction.

My questions are:

  • does 1 month have an equivalent in days ?
  • if yes, is this a common standard in all programing languages ?

    A few examples:

    echo date('Y-m-d',strtotime('2011-03-31 -1 months')); //2011-03-03
    echo date('Y-m-d',strtotime('2011-03-30 -1 months')); //2011-03-02
    echo date('Y-m-d',strtotime('2011-03-29 -1 months')); //2011-03-01
    echo date('Y-m-d',strtotime('2011-03-28 -1 months')); //2011-02-28
    
  • like image 226
    johnlemon Avatar asked Mar 28 '11 19:03

    johnlemon


    People also ask

    How get number of days in a month in php?

    The cal_days_in_month() function returns the number of days in a month for a specified year and calendar.

    Is last day of month php?

    To get the last day of a month, you can use the php date() function and format with “Y-m-t”. “t” returns the last day of a month.

    How do you get the first day of the month in php?

    php $dt = "2008-02-23"; echo 'First day : '. date("Y-m-01", strtotime($dt)).


    2 Answers

    From your examples, it looks like it's subtracting 1 from the month part, and then correcting for illegal dates. Your second example:

    2011-03-30 - 1 month = 2011-02-30. This date does not exist, as February 2011 had only 28 days. 30 - 28 = 2, so it puts it as the 2nd day of the following month.

    However, I have not found documentation about this.

    Either way, assuming I'm right, the answer to your question is no, "1 month" does not have a (constant) equivalent in days, it depends on the input.

    like image 166
    Aistina Avatar answered Oct 06 '22 01:10

    Aistina


    The way strtotime parses date information is going to be very valuable here.

    What you seem to want is the first day of the previous month, right?

    Well, you can chain together many of these relative commands. For example, from the PHP interactive shell:

    php > $d = date_create('2011-03-28 first day -1 month'); if($d) echo $d->format('Y-m-d H:i:s'); else echo "Failed.";
    2011-02-01 00:00:00
    php > $d = date_create('2011-03-29 first day -1 month'); if($d) echo $d->format('Y-m-d H:i:s'); else echo "Failed.";
    2011-02-01 00:00:00
    php > $d = date_create('2011-03-30 first day -1 month'); if($d) echo $d->format('Y-m-d H:i:s'); else echo "Failed.";
    2011-02-01 00:00:00
    php > $d = date_create('2011-03-31 first day -1 month'); if($d) echo $d->format('Y-m-d H:i:s'); else echo "Failed.";
    2011-02-01 00:00:00
    php > $d = date_create('2011-04-01 first day -1 month'); if($d) echo $d->format('Y-m-d H:i:s'); else echo "Failed.";
    2011-03-01 00:00:00
    

    first day asks for the first day of the current month. Asking for -1 month goes to the previous month, on the same day. Because we've already rewound to the first day of the month, this will always work as expected.

    like image 37
    Charles Avatar answered Oct 06 '22 02:10

    Charles