Php populer date bug, could not find a solution. pls help

Hai folks


$chk_dt=date('Y-m', strtotime('-1 month'));
$chk_dt=date("Y-m", strtotime("this month -1 month"));

If you run now, it would return : 2013-11.
but it must be 2013-10.

So whats the fix for this? i could not find a solution in google. please help me.

Why would it be 2013-10? We are in December, one month ago is November…

sorry, again messed up.

actually :


$chk_dt=date('Y-m', strtotime('-1 month'));
$chk_dt=date("Y-m", strtotime("this month -1 month"));

this returns 2013-12 instead of 2013-11 :smiley:

Ah, I was wondering.
I noticed some problems when trying to use “month” years ago, and had some hope it had been worked out by now, apparently not :frowning:

It seems that what I consider to be a month varies, i.e. 30 days, 31 days, 28/29 for February, from what PHP considers to be a “month”, i.e. “30 days” regardless.

That is, “month” seems to work OK most times, except for “beginnings/endings” if you know what I mean.

Perhaps it would be better to use something other than “month”?

Thanks for the explanation to the bug. i understood now. require a concrete solution as my project is all about time sheets :rolleyes:

what about this solution ? it shows correctly.

echo date('Y-m', (strtotime('-1 month')-86400)); // seconds in a day = 86400

That works today because you are on the 31st. But if you were on the first of December, that’d fail too.

agree :slight_smile:

Here is my quick test on a way to resolve this, and it seems to work, but there are a variety of solutions.

If you always need to get to the last day of the prior month, the below code works:

$today = new DateTime('2013-12-31'); // remove the date string if you want to use today's date
$todaysDate = $today->format('d'); // Get today's date, so you know how many days to subtract
$lastDayOfPriorMonth = $today->sub(new DateInterval("P{$todaysDate}D"))->format('Y-m-d'); // Outputs last day of prior month

Here is my test for the entire calendar year of 2013

<?php
$dayOfTest = new DateTime('2013-12-31');
do
{
	$today = new DateTime($dayOfTest->format('Y-m-d'));
	$todaysDate = $today->format('d');
	echo 'Day of Test: ' . $dayOfTest->format('Y-m-d') .
		'One Month Ago: ' . $today->sub(new DateInterval("P{$todaysDate}D"))->format('Y-m-d') .
		'<br />';
	$dayOfTest->sub(new DateInterval('P1D'));
} while ($dayOfTest->format('Y-m-d') != '2012-12-31');

Also note, you can read the “number of days within the given month” to use in the subtraction, but that won’t give you the results you want. As seen in the test below: 2013-12-01 then goes to 2013-10-31.

<?php
$dayOfTest = new DateTime('2012-12-31');
do
{
	$today = new DateTime($dayOfTest->format('Y-m-d'));
	$todaysDate = $today->format('t'); // line changed
	echo 'Day of Test: ' . $dayOfTest->format('Y-m-d') .
		'One Month Ago: ' . $today->sub(new DateInterval("P{$todaysDate}D"))->format('Y-m-d') .
		'<br />';
	$dayOfTest->sub(new DateInterval('P1D'));
} while ($dayOfTest->format('Y-m-d') != '2011-12-31');

Thanks cpradio for the valuable code.
ill check it and feedback.

mean time following code also works today. but not sure any feature bugs will arise.

<?php
$d = new DateTime(date( 'Y-m-d' ));
$d->setDate( $d->format( 'Y' ), $d->format( 'm' ), 1 );
$d->modify( '-1 month' );
echo $d->format( 'Y-m' ), "\
";
?>

2013-11

Yes, that looks like it works in the edge cases too.

Here is my test for the entire calendar year of 2013

<?php
$dayOfTest = new DateTime('2013-12-31');
do
{
	$today = new DateTime($dayOfTest->format('Y-m-d'));
	$todaysDate = $today->format('d');
	echo 'Day of Test: ' . $dayOfTest->format('Y-m-d') .
		'One Month Ago: ' . $today->sub(new DateInterval("P{$todaysDate}D"))->format('Y-m-d') .
		'<br />';
	$dayOfTest->sub(new DateInterval('P1D'));
} while ($dayOfTest->format('Y-m-d') != '2012-12-31');

This code too seems works well for me.

tx for the confirmation

Not a problem, so now you have two pieces, one that will give you the last day of the prior month and the other (which you found) gives you the 1st day of the prior month. Both seem like they may suit you just fine :slight_smile:

ofcause!

HAPPY NEW YEAR .;'}{-+~!#$%^&*()