I'm looking to find the date of the 29th of this month, the 29th of next month, the 29th of last month and so on...
I know you can use this kind of code to find days of the week:
$friday_last = date("d/m/Y", strtotime("last Friday"));
$friday_due = date("d/m/Y", strtotime("this Friday"));
Is there a similar way to find a certain day (29th) of each month or would I have to use a different code?
You need to use DateTime() as it makes working with dates much easier. You'll notice I start by going to the first day of each month. That's so this doesn't break when you get to the 29th-30th of each month as weird date things start to happen.
echo (new DateTime())->modify('first day of this month')->format("29/m/Y");
echo (new DateTime())->modify('first day of previous month')->format("29/m/Y");
echo (new DateTime())->modify('first day of next month')->format("29/m/Y");
Demo
echo (new DateTime())->modify('first day of this month')->modify('-2 months')->format("29/m/Y");
Demo
Using date() with strtotime() will gives you 29th from each month within this year:
<?php
for ($i = 1; $i <= 12; $i++) {
$d = "2016-" . $i . "-29";
echo "29th of Month $i is: " . date("l", strtotime($d)) . '<br>';
}
?>
Output:
29th of Month 1 is: Friday
29th of Month 2 is: Monday
29th of Month 3 is: Tuesday
29th of Month 4 is: Friday
29th of Month 5 is: Sunday
29th of Month 6 is: Wednesday
29th of Month 7 is: Friday
29th of Month 8 is: Monday
29th of Month 9 is: Thursday
29th of Month 10 is: Saturday
29th of Month 11 is: Tuesday
29th of Month 12 is: Thursday
To create an array with current and next 11 months 29th day (for previous months replace +1 month with -1 month. In this example I use +1 to explain february issue, that is not present in past february):
$baseDate = date_create()->modify( 'first day of this month' );
$dates = array();
for( $i = 0; $i<12; $i++ )
{
$newDate = clone $baseDate;
$dates[] = $newDate->modify( '+28 days' );
$baseDate->modify( '+1 month' );
}
The problem — as you can imagine — is with february:
foreach( $dates as $date )
{
echo $date->format( 'Y-m-d' ).PHP_EOL;
}
Will output:
2016-05-29
2016-06-29
2016-07-29
2016-08-29
2016-09-29
2016-10-29
2016-11-29
2016-12-29
2017-01-29
2017-03-01 <-----
2017-03-29
2017-04-29
If you want a result like “29th month's day OR last month's day” modify above for loop in this way:
for( $i = 0; $i<12; $i++ )
{
$newDate = clone $baseDate;
if( $newDate->modify( '+28 days' )->format( 'm' ) != $baseDate->format( 'm' ) )
{
$newDate->modify( 'last day of previous month' );
}
$dates[] = $newDate;
$baseDate->modify( '+1 month' );
}
Result:
2016-05-29
2016-06-29
2016-07-29
2016-08-29
2016-09-29
2016-10-29
2016-11-29
2016-12-29
2017-01-29
2017-02-28 <-----
2017-03-29
2017-04-29
I'm not sure when this started, but now you can do so by using just date() and strtotime(), like this:
$d = date("Y-m-d 00:00:00", strtotime('first day of 2 months ago'));
$l = date("Y-m-d 00:00:00", strtotime('last day of 2 months ago'));
print_r($d);
print_r($l);
Assuming today (2023-02-01) The above will display:
2022-12-01 00:00:00
2022-12-31 00:00:00
Sample:
https://onlinephp.io/c/db73f
Related
I'm trying to get the last weekday of the current month using the code below.
I was expecting this to behave just like all of the other operators like last friday of and output the last weekday of this month. but instead its outputting the last weekday of the previous month.
I know that I can rectify this by using modify( '+1 month' ) but why? Why is it the other operators like last tuesday of work fine but last weekday needs to be bosted by 1 month?
<?php
// outputs the previous months date
$gettoday = date( "Y-m-01" );
$freqdate = date( 'Y-m-d', strtotime( 'last weekday '.$gettoday ) );
echo 'next '.$freqdate;
//get the correct date
$gettoday = new DateTime( $gettoday );
$gettoday->modify( '+1 month' );
$gettoday = date_format( $gettoday, 'Y-m-01' );
$freqdate = date( 'Y-m-d', strtotime( $gettoday . ' last weekday' ) );
echo 'next '.$freqdate;
?>
It appears you had a wrong understanding of what “last” actually means here.
That has nothing to do with the last day or week of the month - it is “last” as in “the closest previous one”.
You can start with something like strtotime('last day of 2020-03') - that would give you 31 Mar 2020, which is a Tuesday.
If you then check what day of the week this is, and it would be either a Saturday or a Sunday - then you can go last friday on that value again, to land on the latest week day of the month.
Edit:
Example for all months of 2020
for($i=1; $i<13; ++$i) {
$year_month = '2020-'.$i;
$last_of_month = strtotime('last day of '.$year_month);
$weekday = date('N', $last_of_month);
if($weekday < 6) {
echo 'last weekday of ', $year_month, ' is ', date('Y-m-d, l', $last_of_month), "<br>\n";
}
else {
$last_weekday_of_month = strtotime('last friday', $last_of_month);
echo 'last day of ', $year_month, ' is ', date('Y-m-d, l', $last_of_month), ' - last weekday of ', $year_month, ' is ', date('Y-m-d, l', $last_weekday_of_month), "<br>\n";
}
}
This will get you the following output:
last weekday of 2020-1 is 2020-01-31, Friday
last day of 2020-2 is 2020-02-29, Saturday - last weekday of 2020-2 is 2020-02-28, Friday
last weekday of 2020-3 is 2020-03-31, Tuesday
last weekday of 2020-4 is 2020-04-30, Thursday
last day of 2020-5 is 2020-05-31, Sunday - last weekday of 2020-5 is 2020-05-29, Friday
last weekday of 2020-6 is 2020-06-30, Tuesday
last weekday of 2020-7 is 2020-07-31, Friday
last weekday of 2020-8 is 2020-08-31, Monday
last weekday of 2020-9 is 2020-09-30, Wednesday
last day of 2020-10 is 2020-10-31, Saturday - last weekday of 2020-10 is 2020-10-30, Friday
last weekday of 2020-11 is 2020-11-30, Monday
last weekday of 2020-12 is 2020-12-31, Thursday
This question already has answers here:
PHP: Adding months to a date, while not exceeding the last day of the month
(7 answers)
Closed 6 years ago.
I have a small problem with dates in PHP.
When I made 31 + 1 month of January
with this code
$newDate = date('Y-m-d', strtotime('31-01-2016'.' + 1 month'));
echo $newDate;
it gives me 2 March but I need given me 29 February,
I need to add 1 month and is not 30days.
ditto for all dates:
for example
01 january + 1 month => 1 february
29 january + 1 month => 29 february
30 january + 1 month => 29 february
31 january + 1 month => 29 february
Thank for your help
I think you are looking for this type of dates.
<?php
$date = date('2016-01-31');
$currentMonth = date("m",strtotime($date));
$nextMonth = date("m",strtotime($date."+1 month"));
if($currentMonth==$nextMonth-1 && (date("j",strtotime($date)) != date("t",strtotime($date)))){
$nextDate = date('Y-m-d',strtotime($date." +1 month"));
}else{
$nextDate = date('Y-m-d', strtotime("last day of next month",strtotime($date)));
}
echo "Next date would be : $nextDate";
?>
Check live demo : https://eval.in/610034
If date is 31-01-2016 then next date would be 29-02-2016
If date is 25-01-2016 then next date would be 25-02-2016
Simply try:
$date = new DateTime('2016-01-31');
$date->modify('last day of next month');
This of course only counts if you always go from the end of one moth to the end of the next one.
try this,
$date = "2016-01-29";
$date = date('Y-m-d', strtotime("last day of next month",strtotime($date)));
echo $date;
https://3v4l.org/Y9PpV
How about something like this:
date_default_timezone_set('UTC');
$current_month = (int) date('m');
$year = date('y');
$newDate = date('Y-m-d', strtotime('31-1-2016'.' + 1 month'));
if($current_month == 12)
{
$new_month=0;
$year++;
}
$d = new DateTime( $year.'-'.($current_month+1).'-01' );
echo $d->format( 'Y-m-t' )."\n";
Change $current_month / $year based on your needs......
I'm using Bootstrap datepicker and I'd like to get special days of every week ( Like: Monday, Wednesday, Saturday). For example, If we select every monday and Wednesday then select only Monday and Wednesday.
And I want this weekday through month wise, (Like, If we select date 24-jun to 24-july and select only Monday. Then every Monday from 24-jun to 24-july will be selected).
So what would be the correct way to get weekdays using bootstrap datepicker?
Any kind of help would be highly appreciated.
Thanks in advance.
I´ve found the solution :
$start_date = "28-06-2016";
$end_date = "28-07-2016";
$weekdays = [1,2]; // 0 = sunday, 1 = monday ...
$range_date = array();
for ($i = strtotime($start_date); $i <= strtotime($end_date); $i = strtotime('+1 day', $i))
{
if(in_array(date('N', $i), $weekdays))//Monday == 1
{
echo date('l Y-m-d', $i).'<br>'; //prints the date only if it's a Monday or tuseday etc..
}
}
And output :
Tuesday 2016-06-28
Monday 2016-07-04
Tuesday 2016-07-05
Monday 2016-07-11
Tuesday 2016-07-12
Monday 2016-07-18
Tuesday 2016-07-19
Monday 2016-07-25
Tuesday 2016-07-26
I am displaying month titles 3 month into the future as well as getting the 1st and last day of each of those months.
for($i = 1; $i < 4; $i++) { // For each month for 3 months
$monthTitle = date('F Y', strtotime('+'.$i.' month'));
$begin_date = date('Y-m-01', strtotime('+'.$i.' month')); // First day of calendar month in future.
$end_date = date('Y-m-t', strtotime('+'.$i.' month')); // Last day of calendar months in future.
};
Nov. 29, 2015 output is:
December 2015
2015-12-01
2015-12-31
January 2016
2016-01-01
2016-01-31
February 2016
2016-02-01
2016-02-29
This was working great right up until yesterday, Nov. 29, 2015 but today Nov. 30, 2015 it skips February.
Nov. 30, 2015 output is:
December 2015
2015-12-01
2015-12-31
January 2016
2016-01-01
2016-01-31
March 2016
2016-03-01
2016-03-31
I'm guessing a bug but does anybody know of a work around?
Thanks to #devlin carnate for pointing me in the right direction.
for($i = 1; $i < 4; $i++) { # for each month
$tmp = date('Y-m-15'); // Get the middle of the month to avoid PHP date bug.
$begin_date = date('Y-m-01', strtotime($tmp . '+'.$i.' month')); // First day of calendar month in future.
$end_date = date('Y-m-t', strtotime($begin_date)); // Last day of calendar months in future.
$monthTitle = date('F Y', strtotime($begin_date));
};
This seems to work very well.
You can use DateInterval to add one month to the current date, so you can get the first and the last day of month.
<?php
$date = new DateTime('2015-12-01');
$i = 0;
while($i < 3){
printf("%s | first day: %s, | last day: %s <br>", $date->format('F Y'), $date->format('d'), $date->format('t'));
$date->add(new DateInterval('P1M'));
$i++;
}
Output:
December 2015 - first day: 01, | last day: 31
January 2016 - first day: 01, | last day: 31
February 2016 - first day: 01, | last day: 29
if last day of next month is needed then you can use this
$d = new DateTime( '2010-01-31' );
$d->modify( 'last day of next month' );
echo $d->format( 'Y-m-d' ), "\n";
Suppose I have a date available with me:
2011-04-05 (i.e., 5th April, 2011)
I want to find date range for Current Week, Month and Year
Current Week: 3rd April to 9th April
Current Month: 1st April to 30 April
Current Year: 1st Jan to 31 Dec
I do understand that current year would be always 1st Jan to 31Dec, but what about current month and week, How can I find it?
Edit:
How can I find date, which is 10 days earlier or later from a given date.
Example:
Suppose today's date is 6th April, 2011
10 day's earlier: 28 March, 2011
10 day's later: 15 April, 2011
Any thoughts on this, guys?
function rangeMonth ($datestr) {
date_default_timezone_set (date_default_timezone_get());
$dt = strtotime ($datestr);
return array (
"start" => date ('Y-m-d', strtotime ('first day of this month', $dt)),
"end" => date ('Y-m-d', strtotime ('last day of this month', $dt))
);
}
function rangeWeek ($datestr) {
date_default_timezone_set (date_default_timezone_get());
$dt = strtotime ($datestr);
return array (
"start" => date ('N', $dt) == 1 ? date ('Y-m-d', $dt) : date ('Y-m-d', strtotime ('last monday', $dt)),
"end" => date('N', $dt) == 7 ? date ('Y-m-d', $dt) : date ('Y-m-d', strtotime ('next sunday', $dt))
);
}
print_r (rangeMonth('2011-4-5')); // format: YYYY-M-D
print_r (rangeWeek('2011-4-5'));
output for rangeMonth()
Array
(
[start] => 2011-04-01
[end] => 2011-04-30
)
output for rangeWeek()
Array
(
[start] => 2011-04-04
[end] => 2011-04-08
)
Notice: functions like getdate(), date(), etc. throw Warning if default time zone is not set in php.ini.
you can use strtotime
example :
date('d.m.Y',strtotime('last day of this month'))
date('d.m.Y',strtotime('last monday')) // for first day of this week
This solution is simple, and it takes in consideration when the current day is Monday or Sunday.
NOTE: Using strtotime('last monday') may work if the day is other than Monday, otherwise it will return the previous Monday. Because of that we should use strtotime('last monday', strtotime('tomorrow')) and it will work for any day of the current week =)
Solution:
$monday = strtotime('last monday', strtotime('tomorrow'));
$sunday = strtotime('+6 days', $monday);
echo "<P>". date('d-M-Y', $monday) . " to " . date('d-M-Y', $sunday) . "</P>";
The following code will give you the start and last date of a week:
$today = getdate();
print_r($today);
echo "<br/>";
$weekStartDate = $today['mday'] - $today['wday'];
$weekEndDate = $today['mday'] - $today['wday']+6;
echo "<br/>";
echo "<br/>";
echo "week start date:".$weekStartDate;
echo "<br/>";
echo "week end date:".$weekEndDate;
Hope it helps...
Check out the getdate function, there are a few examples of how to use it on the manual page I linked. I think it will return everything you're looking for,
Working example it properly handles the Monday issue
<?php
$monday = strtotime("last monday");
$monday = date('w', $monday)==date('w') ? $monday+7*86400 : $monday;
$sunday = strtotime(date("Y-m-d",$monday)." +6 days");
$this_week_sd = date("Y-m-d",$monday);
$this_week_ed = date("Y-m-d",$sunday);
echo "Current week range from $this_week_sd to $this_week_ed ";
?>
Today is Tuesday, August 27th 2019
This week is from Monday, August 26th 2019 to Sunday, September 1st 2019
php > $date = new DateTime('Sunday');
php > echo $date->format('Y-m-d H:i:s');
2019-09-01 00:00:00
php > $date = new DateTime('Tuesday');
php > echo $date->format('Y-m-d H:i:s');
2019-08-27 00:00:00
php > $date = new DateTime('Monday');
php > echo $date->format('Y-m-d H:i:s');
2019-09-02 00:00:00
php > $date = new DateTime('Monday this week');
php > echo $date->format('Y-m-d H:i:s');
2019-08-26 00:00:00