PHP - First and last day of last month.

Posted on 2012-09-02
Last Modified: 2012-09-02
Hi, I need to know the first and the last day of the current month and last month.

I use this:

   function firstOfMonth()
                            return date("Y-m-d H:i:s", strtotime(date('m') . '/01/' . date('Y') . ' 00:00:00'));
                        function lastOfMonth()
                            return date("Y-m-d H:i:s", strtotime('-1 second', strtotime('+1 month', strtotime(date('m') . '/01/' . date('Y') . ' 00:00:00'))));

to find out the first and the last date of the current month, but how do I find out the first and the last day of the last month?

Thank you.
Question by:VAL1N
    LVL 39

    Assisted Solution

    by:Roger Baklund
    The first day of a month should be easy, it is allways "01". So your firstOfMonth() function could have been simplified to something like this:

    date('Y-m-01 00:00:00')

    The PHP date() function has a special format character for finding the last date of a month:

    date('Y-m-t 00:00:00')

    The "t" format character will insert the number of days of the month, i.e. 28, 29, 30 or 31, depending on the month.

    To check previous month (or any month), just provide a date as the second parameter for the date() function:

    date('Y-m-t 00:00:00',strtotime('2012-07-01'))
    LVL 2

    Assisted Solution

    You can use this by passing the $month and $year for finding the first and last date of month.

    $first = date('Y-m-d', mktime(0, 0, 0, $month, 1, $year));
    $last = date('Y-m-t', mktime(0, 0, 0, $month, 1, $year));
    LVL 107

    Accepted Solution



    <?php // RAY_temp_val1n.php
    echo '<pre>';
    // Hi, I need to know the first and the last day of the current month and last month.
    // REQUIRED PHP 5.1+
    function first_of_month($date = 'TODAY') { return date('Y-m-01', strtotime($date)); }
    function  last_of_month($date = 'TODAY') { return date('Y-m-t',  strtotime($date)); }
    echo PHP_EOL . first_of_month();
    echo PHP_EOL . last_of_month();
    echo PHP_EOL . first_of_month('-1 month');
    echo PHP_EOL . last_of_month('-1 month');
    echo PHP_EOL . first_of_month('+1 month');
    echo PHP_EOL . last_of_month('+1 month');

    Open in new window

    HTH, ~Ray

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Article by: Swadhin
    Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
    This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now