• We just launched and are currently in beta. Join us as we build and grow the community.

How to Get Last Day of Month using PHP

Ondakid

Influence Marketer
O Rep
0
0
0
Rep
0
O Vouches
0
0
0
Vouches
0
Posts
137
Likes
153
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
This is a tutorial on how to Get Last Day of Month using PHP. This is a simple tutorial for the beginners who really wants to know how to construct simple source code in PHP. We are going to construct simple PHP source code below for the beginners. So, they can see how to construct it in a simple way. This source code will display how to get the last date of this current month, as well as the last date of a month from the given of a specified date.

We are going to use the letter "t" and that our key to get the last date of a month. In the date() function, the letter "t" put in the date format that can return the number of days in a given month.

Current Month

The current month is September. Take a look the source code below:

  1. <?php
  2. // simple source code to get the last date of this current month.
  3. $last_Day_ofMonth

    =

    date

    (

    'Y-m-t'

    )

    ;
  4. {
  5. ?>
  6. <b>
  7. <?php
  8. // display the last date.
  9. echo

    $last_Day_ofMonth

    ;
  10. ?>
  11. </b>
  12. <?php

    }

    ?>

As you can see the source code above, we used the PHP date() function with the format of "Y-m-t". If you used the source code above, the output will look like this "2016-09-30".

Output

1_161.png

Given Month

This would be the last date from the given of a specified month. The example date is November 13, 2010.

  1. <?php

  2. $date_Given

    =

    '2010-11-13'

    ;

  3. // simple source code to get the last date of this given month.
  4. $last_Day_of_Given_Month

    =

    date

    (

    'Y-m-t'

    ,

    strtotime

    (

    $date_Given

    )

    )

    ;
  5. {
  6. ?>
  7. <b>
  8. <?php
  9. // display the last date of a given month.
  10. echo

    $last_Day_of_Given_Month

    ;
  11. ?>
  12. </b>
  13. <?php

    }

    ?>

In the given date above, we have "2016-11-13" which is the date of November 13, 2010. To get the last date of the given date above, we are going to use the strtotime function. If you run the source code above the result would look like this "2010-11-13". See the image below.

Output

2_75.png

Note: If you only want to get the number of days in a current or given month, kindly omit the "Y" and "m" in the date() function format.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

337,656

337,664

Top