Why does
echo date("j/m/Y", strtotime("2015/01/31 00:00 next month"));
gives
3/03/2015
and not
28/02/2015
All I'm looking for is a todays date next month and if todays date is not valid then it'll give me the last day of next month.
$thisMonth = "2015-01-31 00:00";
$thisMonthDate = strtotime($thisMonth);
$nextMonthDate = strtotime($thisMonth . ' next month');
if (date('j', $thisMonthDate) !== date('j', $nextMonthDate)) {
$nextMonthDate = strtotime(date('Y-m-d H:i:s', $nextMonthDate) . ' last day of previous month');
}
echo date('Y-m-d H:i:s', $nextMonthDate), PHP_EOL;
What PHP is doing here is the following:
first it adds a month to your date, arriving to "2015/02/31"
then it realizes that this date does not exist, and that it is 3 days after "2015/02/28", which, translated in human terms, is "2015/03/03"
Look here for a solution to your problem, as #kingkero suggested
Related
If i have for example :
'2020-01-01'
I want to have two dates :
-The last day of the prévious month :
'2019-12-31'
-The first day of next month :
'2020-02-01'
I tried to use something like echo date("Y-n-31", strtotime('2020-01-01')); but i don't know.
Thank you.
Use below code:
<?php
$month_end = new DateTime("last day of last month");
$month_ini = new DateTime("first day of next month");
echo $month_end->format('Y-m-d'); // will print, Last day of last month
echo $month_ini->format('Y-m-d'); // will print First day of next month
?>
With Datetime object with user defined date (Custom date)
# with datetime object
$d1 = new DateTime('2019-12-01');
$d1 -> modify('last day of last month');
echo $d1 -> format('d.m.Y'), "\n";
$d2 = new DateTime('2019-12-01');
$d2 -> modify('first day of next month');
echo $d2 -> format('d.m.Y'), "\n";
Output:
30.11.2019
01.01.2020
Try this, Use strtotime() for add month or minus month
$date='2020-01-01';
echo date("Y-m-t", strtotime('-1 month'.$date)); //2019-12-31
echo date("Y-m-01", strtotime('+1 month'.$date)); //2020-02-01
strtotime()
this php function convert english textual date-time discription into UNIX timestamp
the first step converts textual date-time into a timestamp
now you have timestamp then use the date() function
-The last day of the prévious month :
echo date("Y-m-d", strtotime("Last day of last month"));
-The first day of next month :
echo date("Y-m-d", strtotime("First day of next month"));
You can use DateTime's method modify() to achieve relative dates:
<?php
$dateOld = new \DateTimeImmutable('2020-01-01');
echo $dateOld->modify("last day of last month")->format('Y-m-d');
echo PHP_EOL;
echo $dateOld->modify("first day of next month")->format('Y-m-d');
Try it here: https://3v4l.org/qALa5
$date='2020-01-01';
echo date("Y-m-d", 'last day of previous month', strtotime($date)));
echo date("Y-m-d", 'first day of next month', strtotime($date)));
I need to get the next month with php. Everywhere is written these example
date('Y-m-t', strtotime('+1 month'));
The output of the above code is '2017-03-31'. I need to get February, not March.
If you want to get the next irrespective of the current date in the current month. below code may help you
echo date('M',strtotime('first day of +1 month'));
// e.g. "Jan"
echo date('m',strtotime('first day of +1 month'));
// e.g. "1"
echo date('F',strtotime('first day of +1 month'));
// e.g. "January"
This will give you Next month.
You can find more formatting masks in date() function documentation
Use Like This
// One month from today
$date = date('Y-m-d', strtotime('+1 month'));
// One month from a specific date
$date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01')));
To get next month using php use string to time function as below:
$nxtm = strtotime("next month");
echo date("M", $nxtm);
date("m", strtotime("2021-08-16 +1 Month"));
You can use the code below to get the next months first day.
$date = (new \DateTime('first day of next month'))->format('Y-m-d');
This is safer than 'next month' or '+1 month' because you may skip some months then.
If you want to have the next month with the same day as this month, or the last day of the next month if it otherwise would switch month you can use this function
function nextMonth()
{
$nextMonthNumber = date('M', strtotime('first day of +1 month'));
$nextMonthDate = new DateTime();
$nextMonthDate->add(new DateInterval('P1M'));
while ($nextMonthDate->format('M') != $nextMonthNumber) {
$nextMonthDate->sub(new DateInterval('P1D'));
}
return $nextMonthDate;
}
A client wants a newsletter to be automatically generated every Monday, showing the schedule for the upcoming week. That's easy:
if(date('N', $time)==1) { /* Stuff */ }
Attach that to a crontab running nightly and I'm good to go.
However, if the newsletter is being generated in the last week of the month, it needs to show the schedule for the upcoming month. How would I determine when the monthly schedule needs to be generated?
date('m') == date('m', strtotime('+1 week'))
If the month a week from the date the report is running is different than the current month, show the report!
if(date('n', $time) !== date('n', $time+518400)){
// Six days from now it will be a new month
}
One way might be to see if next Monday is in a different month.
if (date('n', $time) != date('n', $time + 7*24*60*60)) { ... }
You could be fancier, but this seems consistent with your existing code.
You can use the t date format param to see how many days are in the particular month. Try
if ((date('t', $time) - date('j', $time)) > 6) {
// in the last week of the month
}
I know this a answered but this will help more:
PHP's built-in time functions make this even simple. http://php.net/manual/en/function.strtotime.php
// Get first Friday of next month.
$timestamp = strtotime('first fri of next month');
// Get second to last Friday of the current month.
$timestamp = strtotime('last fri of this month -7 days');
// Format a timestamp as a human-meaningful string.
$formattedDate = date('F j, Y', strtotime('first wed of last month'));
how to find date of last thursday of the month in php ?
it is for the payment thing. staffs needs to paid on last thursday of the month on which they have submitted invoices. I am having hard time finding out the date of last thursday of month
thanks
abhinab
PHP >= 5.3:
<?php
$date = strtotime('last thu of this month');
echo date('d.m.Y H:i:s', $date);
PHP < 5.3:
<?php
$date = strtotime(sprintf('+1 month %s %s', date('F'), date('Y')));
while (date('D', $date) !== 'Thu') {
$date -= 86400;
}
echo date('d.m.Y H:i:s', $date);
(didn't find a better way to do this)
Output:
30.06.2011 00:00:00
Find the first day of the month right after.
Walk backwards one day at a time until you hit a Thursday.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Get first day of week in PHP?
Hi,
I want to find first and last date of current week and last week.
Similarly I want to find first and last date of current month and last month.
This has to be done in PHP. Please help.
strtotime is quite powerful with relative time formats:
strtotime('monday this week');
strtotime('sunday this week');
strtotime('monday last week');
strtotime('sunday last week');
(this only works with PHP 5.3+)
strtotime('first day of this month');
strtotime('last day of this month');
strtotime('first day of last month');
strtotime('last day of last month');
In order to get the first and last date of a month in PHP < 5.3, you can use a combination of mktime and date (date('t') gives the number of days of the month):
mktime(0,0,0,null, 1); // gives first day of current month
mktime(0,0,0,null, date('t')); // gives last day of current month
$lastMonth = strtotime('last month');
mktime(0,0,0,date('n', $lastMonth), 1); // gives first day of last month
mktime(0,0,0,date('n', $lastMonth), date('t', $lastMonth); // gives last day of last month
If you just want to get a string for presentation, then you don't need mktime:
date('Y-m-1'); // first day current month
date('Y-m-t'); // last day current month
date('Y-m-1', strtotime('last month')); // first day last month
date('Y-m-t', strtotime('last month')); // last day last month
Here's a function for the first and last day of the week:
function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}
$sStartDate = week_start_date($week_number, $year);
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));
It can probably be adapted to do month as well, but I wanted to get my answer in! :)