2

I'm currently using DateTime objects to perform some calculations involving dates. For this instance, I'm trying to get the last day of the month using a pre-existing date. I tried the following code and wound up with an incorrect value for $endDate, that being 1970-1-1.

$startDate = new DateTime(date("Y-m-1"));
$endDate = new DateTime(date("Y-m-d", strtotime("last day of month", $startDate->format("Y-m-d"))));

echo "The start date should be 2016-1-1: " . $startDate->format("Y-m-d") . "<br />";
echo "The end date should be 2016-1-31: " . $endDate->format("Y-m-d") . "<br />";

How can I get it working correctly so that $endDate comes out to the desired result? I don't want it to be this month specifically; it should work for any date string I supply via $startDate.

Leon Adler
  • 2,993
  • 1
  • 29
  • 42
user3521737
  • 283
  • 1
  • 4
  • 17
  • Do you get the last day in a month or the name of the last day in a month? You can use `cal_days_in_month(CAL_GREGORIAN, $month, $year)`. The last day is the number of days too – Giancarlo Ventura Granados Jan 11 '16 at 17:34
  • I just tried this and I'm still getting the same results. – user3521737 Jan 11 '16 at 17:36
  • 1
    Possible duplicate of [How to find the last day of the month from date?](http://stackoverflow.com/questions/1686724/how-to-find-the-last-day-of-the-month-from-date) – aynber Jan 11 '16 at 17:39

4 Answers4

0

strtotime expects the second parameter to be a timestamp. Try this instead:

$endDate = new DateTime(date("Y-m-d", strtotime("last day of month", $startDate->getTimestamp())));
aynber
  • 22,380
  • 8
  • 50
  • 63
0

Use char "t" in your datetime as it is the number of days in the given month

$endDate = new DateTime(date("Y-m-t"));
Yim
  • 31
  • 3
  • If anything, it's now showing the correct DAY, but it's still the wrong year of 1970. – user3521737 Jan 11 '16 at 17:43
  • And the $startDate is showing correct year? Because I've just tried this on my machine and it results in correct result – Yim Jan 11 '16 at 17:46
0

This page may have the answer you seek: How to find the last day of the month from date?

Applied to your code:

$startDate = new DateTime(date("Y-m-1"));
$endDate = new DateTime(date("Y-m-t", $startDate->getTimestamp()));

echo "The start date should be 2016-1-1: " . $startDate->format("Y-m-d") . "<br />";
echo "The end date should be 2016-1-31: " . $endDate->format("Y-m-d") . "<br />";
Community
  • 1
  • 1
Christopher Stevens
  • 1,214
  • 17
  • 32
  • This is only a partial solution. The dates are intended to be from last month, so I need to be able to use $startDate to generate $endDate (if $startDate is 2015-3-1, $endDate needs to be 2015-3-31). – user3521737 Jan 11 '16 at 17:42
  • Ah, see updated code in this answer. This generates a proper end of month date for whatever month is specified in $startDate. If you wanted to specify "last month", you could alter $startDate to: `$startDate = new DateTime(date("Y-m-d", strtotime("first day of previous month")));` – Christopher Stevens Jan 11 '16 at 17:47
  • ...and I edited one more time to use DateTime's getTimestamp() instead of converting from a DateTime to a string to a timestamp... – Christopher Stevens Jan 11 '16 at 17:55
  • You made a small error. Where you assign $endDate, you put `Y-m-1` when it should be `Y-m-d`. – user3521737 Jan 11 '16 at 17:58
  • Actually, should have been Y-m-t (thank you others posting here, I'll check my work next time) – Christopher Stevens Jan 11 '16 at 18:09
0

Fixed it using some of the suggestions below.

$startDate = new DateTime(date("2012-4-1"));
$endDate = new DateTime(date("Y-m-t", strtotime($startDate->format("Y-m-d"))));

echo "The start date should be 2012-4-1: " . $startDate->format("Y-m-d") . "<br />";
echo "The end date should be 2012-4-30: " . $endDate->format("Y-m-d") . "<br />";

I've verified that this works.

user3521737
  • 283
  • 1
  • 4
  • 17