How to find a start date & end date of any given year & month

前端 未结 8 1453
庸人自扰
庸人自扰 2021-01-04 05:18

I have problem in php find start date & end date of month & year , when i know the year and month ?

ex:

input - > year = 2011 , month = 0         


        
相关标签:
8条回答
  • 2021-01-04 05:51

    i really can't understand you clearly but to get the start date here is the code

    date('Y-m-d');
    

    this code above will get you the day of today and to get the end of the running month this code i used before

    date(’Y-m-d’,strtotime(’-1 second’,strtotime(’+1 month’,strtotime(date(’m').’/01/’.date(’Y').’ 00:00:00′))));
    

    i hope this help you in your issue

    0 讨论(0)
  • 2021-01-04 05:59

    Use date (t format gives days in year) and create a time for it:

    $year = 2011; $month = 6;
    
    $starts = 1;
    $ends = date('t', strtotime($month.'/'.$year)); //Returns days in month 6/2011
    
    0 讨论(0)
  • 2021-01-04 06:02
    echo date('m-01-Y 00:00:00',strtotime('this month')) . '<br/>';
    echo date('m-t-Y 12:59:59',strtotime('this month')) . '<br/>';
    
    0 讨论(0)
  • 2021-01-04 06:02
    $year = '2017';
    $month = '05';
    echo date("$year-$month-01");
    echo "<br>";
    echo date("$year-$month-t");
    

    shortest solution in my own opinion.

    0 讨论(0)
  • 2021-01-04 06:09

    PHP may have a more elegant way of doing this, but if you want a generic algorithm, here's what you need to do...

    All months other than February have a fixed number of days. February has 29 only when it's a leap year. Here are the rules to check if it's a leap year:

    1. If the year is evenly divisible by 4, go to step 2. Otherwise, go to step 5.
    2. If the year is evenly divisible by 100, go to step 3. Otherwise, go to step 4.
    3. If the year is evenly divisible by 400, go to step 4. Otherwise, go to step 5.
    4. The year is a leap year (February has 29 days).
    5. The year is not a leap year (February has 28 days).
    0 讨论(0)
  • 2021-01-04 06:11

    You should look into strtotime:

    echo date("D, M j, Y", strtotime("FIRST DAY OF MAY 2012"));
    // Tue, May 1, 2012
    echo date("D, M j, Y", strtotime("last DAY june 2012")); // gotcha! using June.
    // Thu, May 31, 2012
    
    0 讨论(0)
提交回复
热议问题