Friday, March 18, 2016

How to get the date of first day of previous month in php

How to get the date of  first day of previous month in php:

For getting the date of first day of previous month in php we have to use this code.


    public function getDate()
    {
        $curMonth = date('m', strtotime(date('Y-m-d')));
        $curYear = date('Y', strtotime(date('Y-m-d')));
        $occDate="$curYear-$curMonth-01";
        $forOdNextMonth= date('m', strtotime("-1 month", strtotime($occDate)));
        $forOdNextYear= date('Y', strtotime("-1 month", strtotime($occDate)));
        return "$forOdNextYear-$forOdNextMonth-01";
    }



Using this code we can get the desire result.

It will be helpful for getting the previous year even in December month which usually shows some problem for fetch.

Thanks

No comments:

Post a Comment