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 8419801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:42:50+00:00 2026-06-10T02:42:50+00:00

When using php DateTime object to get date of the last day of the

  • 0

When using php DateTime object to get date of the last day of the month, the year is always incorrect.

My code is:

$d = new DateTime();
$d->modify('last day of this month');
$formattedDateTime = $d->format('Y-m-d H:i:s')

Everything is correct apart from the year which is being returned as 7168 instead of 2012.

Anyone any ideas as to what is causing this?

  • 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-10T02:42:52+00:00Added an answer on June 10, 2026 at 2:42 am

    As others have said, it works for me also (PHP 5.4.3). There have been some bugs in the date/time code over some of the more recent PHP versions, though, so try upgrading your installation if possible and see if that helps.

    That being said, you should just be passing the string directly to the constructor instead of using DateTime::modify():

    $d = new DateTime('last day of this month');
    $formattedDateTime = $d->format('Y-m-d H:i:s');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been using the DateTime class in PHP, because date() has the downsides of
I'm using PHP/MySQL and I've always used DATETIME to store created and updated values,
Is it possible to get the first / last date of a week using
Using php I am trying to get some data from a .csv file which
I am experiencing a rather strange problem using PHP 5.3's date diff function to
How do I get a time countdown using PHP? I want to display something
How to retrieve the last updated data from multiple categories using php and mysql.
I am new to taking an object oriented approach with php...actually I have a
I am having a weird problem with PHP's DateTime diff method. I've been using
When building PHP applications I always end up having trouble trying to get everything

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.