LinkShare  Referral  Program

New

Advertise ad Here
learn more
            PHP > date manipulation functions>End of the month
Added on   06.09.09
Clicks     2090
Rating:   4.20   out of 5 from   5   raters      
         rate this page   
 Rate this page,Please

iBuyOfficesupply.com Inc. There is not Comments for this Article,Be the first one
Ask any relevant question in the Comments or leave your valuable feed-back

printer  Print version of this article

star  Add it to your favourite!

mail email this page to a frend
LinkShare  Referral  Program

Date and time manipulation functions



 Find end of the month function




 

 

    Finding the end of the month from today's date can be very useful specially for commercial business that need to work out future payments. For example ,if you have an invoice due to be paid in 30 day from the issue but extended to the end of that month this function is just what you need    .



    We assume that all the functions will be saved in a file called php_functions.php

    <?php
    function last_day_of_month($month, $format)
    {

    $today=today(8,'d-m-Y');
    $date=explode('-',$today) ;

    $first_second_this_month = $date[2].'-'.$date[1].'-01'.' 00:00:00';
    $last_second_next_month = strtotime("-1 second", strtotime("+".$month." months", strtotime($first_second_this_month)));
    $end_of_month=date($format, $last_second_next_month);
    return $end_of_month;

    }
    ?>

    Explaining the above function is quite simple, as you can see ,it has 2 parameters,
    $month is where you enter the number of month from today, that you want to find the end,for example,if today is the 10th of September 2009 and you enter 2 it'll output the 31st October 2009.
    $format is where you put the format you want the date to appear in your page, for complete formatting details, visit the PHP manual page.

    $today=today(8,'d-m-Y'); The variable $today will hold the actual todays date with your time zone which in this case is 8 but you can edit to correspond to your time zone.

    $date=explode('-',$today) ;$date will hold the array resulting by exploding $today

    $first_second_this_month = $date[2].'-'.$date[1].'-01'.' 00:00:00'; this is quite clear

    $last_second_next_month = strtotime("-1 second", strtotime("+".$month." months", strtotime($first_second_this_month))); We need this to bring back the formatted date into a timestamp to calculate the end of month.

    $end_of_month=date($format, $last_second_next_month); Here is where the timestamp will be formatted in a readable date with the format of your choice.

    See the example below to use the function

    <?php
    include 'php_functions.php';

    $format='d-m-Y';

    $end=last_day_of_month(2,$format);
    echo $end
    //if today's date is 10 Sep 2009 will output 31-10-2009
    ?>

     

    Try the example below

     Today date is  12-03-10
     Number of Month ahead  
     Your time zone  
     The format you want  
     End of your chosen month is