What happening to php date function [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP date() and strtotime() return wrong months on 31st
I have this code and it outputs something strange i think. So, what i am doing wrong here.
<?php
$sP1 = date('m Y');
$sP2 = date('m Y', strtotime('+01 month'));
$sP3 = date('m Y', strtotime('+02 month'));
$sP4 = date('m Y', strtotime('+03 month'));
echo $sP1.'<br>';
echo $sP2.'<br>';
echo $sP3.'<br>';
echo $sP4.'<br>';
?>
and this outputs
05 2012
07 2012
07 2012
08 2012
i think the second one should be
06 2012
Anybody know any solution?

Today is the 31st next month only has 30 days so it would be 7/12 in 1 month from today
assuming that today is May 31 2012
date('m Y') == 05 2012
date('m Y', strtotime('+1 month')) == 07 2012 because june has 30 days
date('m Y', strtotime('+2 month')) == 07 2012
date('m Y', strtotime('+3 month')) == 08 2012
date('m Y', strtotime('+4 month')) == 10 2012
I would take today's date and find the first day of the month then add a month to that if you are doing something that needs to get each month

As others have said, it is because today is the 31st and +1 month equals June-31 which changes to Jul-1. If you include the day in the date string, you can see exactly this.
<?php
$sP1 = date('m-d-Y');
$sP2 = date('m-d-Y', strtotime('+01 month'));
$sP3 = date('m-d-Y', strtotime('+02 month'));
$sP4 = date('m-d-Y', strtotime('+03 month'));
echo $sP1."\n";
echo $sP2."\n";
echo $sP3."\n";
echo $sP4."\n";
/* Outputs:
05-31-2012
07-01-2012
07-31-2012
08-31-2012
*/
?>
strtotime though can take the start date as part of the string so as King suggested, calculate the +N months from the first. So a string like May-1-2012 +01 month such as:
<?php
$sP1 = date('m Y');
$sP2 = date('m Y', strtotime(date('M-1-Y').' +01 month'));
$sP3 = date('m Y', strtotime(date('M-1-Y').' +02 month'));
$sP4 = date('m Y', strtotime(date('M-1-Y').' +03 month'));
echo $sP1."\n";
echo $sP2."\n";
echo $sP3."\n";
echo $sP4."\n";
/* Outputs:
05 2012
06 2012
07 2012
08 2012
*/
?>
http://codepad.org/auYLHvDI

It is working as intended. In a nutshell, it is because what is "one month" from May 31? June 30? August 1?
My suggestion is that if you need sequential months, calculate the offset from the start of the current month, not the current day. Or compose the date that you're looking for manually using the month, day, and year parts broken up.

Related

Display date that changes every 15th day of the month

I'm relatively new to php and is currently looking for a way to display a date that adds 1 month on current date and adds 2 months every 15th day of every month.
For example:
Current Date: January 13, 2022
Display Date: February 2022
on January 15, 2022 the date to be displayed is March 2022 (which will be the displayed until February 14, 2022)
on February 15, 2022 the date to be displayed is April 2022
<?php
$today = date("D");
$date = date("F Y", strtotime(" +1 months"));
if ($today >= "15") {
$d=strtotime("+2 Months");
echo date("F Y", $d);
} else {
echo $date;
}
?>
Thanks in advance for your help.
Your code is almost fine.
data("D") instead of returning 15 will return the day string of the week (mon, wed, etc) so when you compare date("D") with 15 you are comparing "mon" with 15.. (not right). You have to use "d" instead (lowercase d)
This is my version of your same code
if (date("d") >= 15) {
$show_date = date("F Y", strtotime("+2 Month"));
} else {
$show_date = date("F Y", strtotime("+1 Month"));
}
echo $show_date;
Or if you prefer 1 line solution:
echo date("F Y", strtotime("+" . (date("d") >= 15 ? 2 : 1) . " Month"));

Getting the three previous months in PHP is showing the same result [duplicate]

This question already has answers here:
how to get the previous 3 months in php
(4 answers)
Closed 2 years ago.
I need the three previous months from the current month.
I got this code from Stack Overflow here:
echo date('M Y', strtotime('-0 month'));
echo date('M Y', strtotime('-1 month'));
echo date('M Y', strtotime('-2 month'));
echo date('M Y', strtotime('-3 month'));
The result is supposed to be:
Mar 2020
Feb 2020
Jan 2020
Dec 2019
But, I'm getting:
Mar 2020
Mar 2020
Jan 2020
Dec 2019
What's the problem? Is it because of the leap year in February?
I am also new to this concept, but this is what I found and it seems to work.
When you use strtotime('-1 month'), you would get February 31. This day doesn't exist, so that's probably what causes the issue.
I found this format that would get the first day of the month, that should fix the problem. It might not be the cleanest way to do it, but this seems to work.
echo date('M Y', strtotime('first day of -0 month'));
echo date('M Y', strtotime('first day of -1 month'));
echo date('M Y', strtotime('first day of -2 month'));
echo date('M Y', strtotime('first day of -3 month'));
By writing "first day of" before the -* month, you will grab the first day instead.
Check out Relative Formats where you can find all the formats you can use.
"is it because of the leap year february?' - Yes it does have something to do with it.
Per the manual on strtotime():
from user contributed note:
Depending on the day of the month, you may get a different response. For a non-leap year, you'll get March if the current day of the month is the 29th, 30th or 31st. If it's a leap year, you'll get March on the 30th or 31st of the month. The same thing will happen on the 31st of any month when you pass in the name of any month with less than 31 days. This happens because the strtotime() function will fill in missing parts from the current day.
Well, I suggest you to use the DateTime library of PHP.
Like this:
$today = new DateTime();
$today->modify('last day of previous month');
echo $today->format('M') . '\n';
$today->modify('last day of previous month');
echo $today->format('M') . '\n';
$today->modify('last day of previous month');
echo $today->format('M') . '\n';
You will have these answers:
Feb
Jan
Dev
For more details, see DateTime::modify

How To Get Next Wednesday With PHP?

I have this situation :
17 January 2017 is Tuesday.
I'm expecting my code will generate 25 January 2017 as NEXT Wednesday. Not 18 January 2017.
19 January 2017 is Thursday.
I'm expecting my code will generate 25 January 2017 as NEXT Wednesday too.
but this code :
$payment_date = '17 January 2017';
echo $payment_date . '<br>';
$payment_date = date('d M Y', strtotime('next Wednesday', strtotime($payment_date)));
echo $payment_date;
gives me 18 January 2017 as next Wednesday. how to get 25 January 2017 as next Wednesday when my code runs between 15 - 21 January 2017?
thank you
$payment_date = date('d M Y', strtotime('next wednesday next week', strtotime($payment_date)));
Try using +1 week Wednesday instead of Next Wednesday:
$payment_date = date('d M Y', strtotime('+1 week Wednesday', strtotime($payment_date)));

Parsing dates with inconsistent formats in PHP

I have two queries, both related to dates.
1) I have dates in these formats, which I'm looking to normalise into the same format before saving into a database:
Saturday 26 July
Monday 28 - Wednesday 30 July
July 24th, 2014
Thu 4 Sep
Thu 28 Aug — Fri 19 Sep
24-07-2014
Single days are quite easy to work out using strtotime(), but ranges of dates are a bit more tricky.
This, for example, doesn't work:
$dateString = "Monday 28 - Wednesday 30 July";
if (strpos($dateString, "-")) {
$datePieces = explode("-", $dateString);
$startDate = strtotime($datePieces[0]);
$endDate = strtotime($datePieces[1]);
} else {
$startDate = strtotime($dateString);
$endDate = strtotime($dateString);
}
echo '<pre>';
echo date('d F Y', $startDate);
echo '<br/>';
echo date('d F Y', $endDate);
Because the month is only on one side of the explode(), doing it this way returns:
01 January 1970
30 July 2014
2) I need a way of working out what year the date is (it will always be in the future). Something along the lines of:
if (the month in the date string has elapsed) {
the year of the date is this year + 1
}
As long as each source provides you with a consistent format you can use DateTime() and DateTime::createFromFormat() to process the dates for you.
//Saturday 26 July
$date = DateTime::createFromFormat('l j F', 'Saturday 26 July');
//July 24th, 2014
$date = new DateTime('July 24th, 2014');
//Thu 4 Sep
$date = DateTime::createFromFormat('D j M', 'Thu 4 Sep');
//Thu 28 Aug — Fri 19 Sep
list($start, $end) = explode(' - ', 'Thu 28 Aug — Fri 19 Sep');
$start = DateTime::createFromFormat('D j M', $start);
$end = DateTime::createFromFormat('D j M', $end);
//24-07-2014
$date = new DateTime('24-07-2014');
I'm going to leave handling Monday 28 - Wednesday 30 July to you since you'll need to do a little more work to get the month from the second date and apply it to the first. But this should show you how to go about this.

Convert date to two days from a given date

I'm trying to convert a given date to a date that's two days ahead of the given date. My code is as follows:
$date = date('D, M n', strtotime('+2 days', 'Mon, Dec 31, 2012'));
That code sort of gets it correct. It echoes "Wed, Jan 1". It gets the name of the day and the month correct. But, not the date. I have also tried another route.
$d = new DateTime('Mon, Dec 31, 2012');
$d->modify('+2 days');
echo $d->format('D, M n');
That didn't work either. Any ideas?
Thanks,
Lance
n is the format flag for month month. It's saying 1 because it's in January. Use j instead:
$d = new DateTime('Mon, Dec 31, 2012');
$d->modify('+2 days');
echo $d->format('D, M j'); //Wed, Jan 2
$newdate = date("D, M n",strtotime($oldDate. ' + 2 day'));

Categories