Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:29:16+00:00 2026-06-18T04:29:16+00:00

How to get last day of each month within a date range in PHP?

  • 0

How to get last day of each month within a date range in PHP?

Input:

$startdate = ‘2013-01-15’
$enddate = ‘2013-03-15’

The output that I want is:

2013-01-31 End date of January
2013-02-28 End date of February
2013-03-15 *End date of March is ‘2013-03-31’ but the end date is ‘2013-03-15’ .

So I want 2013-03-15.

How can I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:29:18+00:00Added an answer on June 18, 2026 at 4:29 am
    function get_months($date1, $date2) {
    
        $time1 = strtotime($date1);
        $time2 = strtotime($date2);
    
        $my = date('mY', $time2);
        $months = array(date('Y-m-t', $time1));
        $f = '';
    
        while($time1 < $time2) {
            $time1 = strtotime((date('Y-m-d', $time1).' +15days'));
    
            if(date('F', $time1) != $f) {
                $f = date('F', $time1);
    
                if(date('mY', $time1) != $my && ($time1 < $time2))
                    $months[] = date('Y-m-t', $time1);
            }
    
        }
    
        $months[] = date('Y-m-d', $time2);
        return $months;
    }
    
    $myDates = get_months('2005-01-20', '2005-11-25');
    

    $myDates will have the output you want. It will work even if years are different. Logic is from this URL

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to get the last day of the month given as a date
i have to get the last day of the current month. how can i
How do you get the last day of the last month in csh? Here
I have some date and I want to get last x day before this
How can I get last year's start and end date using PHP code? Is
So I'm using this code to get the last entry for each day from
I need to get the last 90 hired employees that were terminated for each
There is an example on php.net how to get last two domain segments in
I need to get a string in perl whose length is varying each day.
I have an interesting problem that involves taking the last day from a matrix

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.