Missing months with less than 31 days (strtotime) [duplicate] - php

This question already has answers here:
Months with less than 31 days omitted from list
(3 answers)
Why does PHP date('m', strtotime('-1 months')) not work correctly for today? 07/31
(5 answers)
Month duplication occurs when DateTime called on end of a 31 day month
(1 answer)
Closed 5 years ago.
I want to output the last 13 months, beginning with last month.
my code:
for($i=1;$i<=(13);$i++)
{
echo date("m",strtotime("-".$i." month"));
}
It worksfine, but today (31.) it looks like this
07
07
05
05
03
03
01
12
12
10
10
08
07
I am missing the months, that do not have 31 days. How can I fix this?

Works with a DateTime object! ;-)
$date = new DateTime('2017-08-31');
for ($i=1; $i<=(13); $i++) {
$tmpDate = clone $date;
$tmpDate->modify('last day of -' . $i . ' month');
echo $tmpDate->format('Y-m-d') . "\n";
}
The trick is to copy the variable so that you can reduce the months times var $i. Using "last day of -x month" always picks the last day of that month.
Reference: https://secure.php.net/manual/en/class.datetime.php

Related

finding next month, previous month giving wrong result [duplicate]

This question already has answers here:
Months with less than 31 days omitted from list
(3 answers)
Closed 5 years ago.
I want to find 2 months after current month and previous month. I have coded as below,
$current = strtotime(date('m',time()) . '-01 00:00:01');
$next_month = date('m', strtotime('+1 month', $current));
$next_next_month = date('m', strtotime('+2 month', $current));
$previous_month = date('m', strtotime('-0 month', $current));
echo $previous_month.$next_month.$next_next_month;
output : 01 02 03
I want to get the result from current month. And the expected output is 04 05 06.
How to achieve this result. Is there any solution. Thankyou.
$m = date('m');
printf("%02d %02d %02d", ($m+10) % 12 + 1, $m, $m % 12 + 1);
Output (on 2nd April):
03 04 05

How to get month number by week number and year [duplicate]

This question already has answers here:
Month by week of the year?
(4 answers)
Closed 6 years ago.
I want to find the month number by given year and given week number,
example- if my year: 2017 and week number is: 25 what will be the month.
Try this:
$date = new DateTime( "01.01.2017" ); // set datetime to 1, January 2017
$date->modify( "+21 weeks" ); // add 21 weeks
echo $date->format( "m" ); // echo month

Add 30 days to date [duplicate]

This question already has answers here:
Add number of days to a date
(20 answers)
Closed 6 years ago.
Hello all i am trying to add 30 days to my date. I am using below coding.
<?php
$next_due_date = date('05/06/2016', strtotime("+30 days"));
echo $next_due_date;
?>
But it is returning to "05/06/2016" only!
Please help me!
Do not use php's date() function, it's not as accurate as the below solution and furthermore it is unreliable in the future.
Use the DateTime class
<?php
$date = new DateTime('2016-06-06'); // Y-m-d
$date->add(new DateInterval('P30D'));
echo $date->format('Y-m-d') . "\n";
?>
The reason you should avoid anything to do with UNIX timestamps (time(), date(), strtotime() etc) is that they will inevitably break in the year 2038 due to integer limitations.
The maximum value of an integer is 2147483647 which converts to Tuesday, 19 January 2038 03:14:07 so come this time; this minute; this second; everything breaks
Source
Another example of why I stick to using DateTime is that it's actually able to calculate months correctly regardless of what the current date is:
$now = strtotime('31 December 2019');
for ($i = 1; $i <= 6; $i++) {
echo date('d M y', strtotime('-' . $i .' month', $now)) . PHP_EOL;
}
You'd get the following sequence of dates:
31 December
31 November
31 October
31 September
31 August
31 July
31 June
PHP conveniently recognises that three of these dates are illegal and converts them into its best guess, leaving you with:
01 Dec 19
31 Oct 19
01 Oct 19
31 Aug 19
31 Jul 19
01 Jul 19
Please try this.
echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) . PHP_EOL;
This will return 06/06/2016. Am assuming your initial date was in m/d/Y format. If not, fret not and use this.
echo date('d/m/Y',strtotime('+30 days',strtotime(str_replace('/', '-', '05/06/2016')))) . PHP_EOL;
This will give you the date in d/m/Y format while also assuming your initial date was in d/m/Y format. Returns 05/07/2016
If the input date is going to be in mysql, you can perform this function on mysql directly, like this.
DATE_ADD(due_date, INTERVAL 1 MONTH);
The first parameter is the format, not the current date.
<?php
$next_due_date = date('d/m/Y', strtotime("+30 days"));
echo $next_due_date;
Demo: https://eval.in/583697
If you want to add the 30 days to a particular starting date use the second parameter of the strtotime function to tell it where to start.
When in doubt about how a function works refer to the manual.
http://php.net/manual/en/function.strtotime.phphttp://php.net/manual/en/function.date.php
You need to provide date format like d/m/Y instead of 05/06/2016
Try
$old_date = '05-06-2016';
$next_due_date = date('d-m-Y', strtotime($old_date. ' +30 days'));
echo $next_due_date;
$date = "1998-08-14";
$newdate = strtotime ( '30 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;
Found the above code

GET ISO WEEK NO from month and week no [duplicate]

This question already has answers here:
Calculating days of week given a week number
(14 answers)
Closed 6 years ago.
I am getting month and its weekno like 1,2,3,4,5 . I want ISO week from it.
Eg. I have April as month and week no 4. I want weekno 16 as output.
Thanks in advance.
You can instantiate a new DateTime using the month, increment by the number of weeks, and use the W date format.
<?php
$month = 'April';
$week = 4;
$date = new DateTime("$month 01");
$date->modify("+$week weeks");
echo $date->format('W');
As far as I can tell, the 4th week of April would be ISO week 17, not 16 though.
date("W"); Will return the week number.
http://php.net/manual/en/function.date.php
Use mktime to set a specific date / time and you'll be sorted!

counting no. Of days categorized in months [duplicate]

This question already has answers here:
How to count days between two dates in PHP? [duplicate]
(11 answers)
Closed 8 years ago.
counting no. Of days between two dates inclusive of starting date is easy but how to count them saperately for each months like between 1.1.2014 and 15.5.2014
Jan 31
Feb 28
Mar 31
Apr 30
May 15
I think you are looking for something like this:
$start = new DateTime('2014-01-01');
$end = new DateTime('2014-05-15');
$tmpStart = $start;
while ( $tmpStart <= $end ) {
$tmpEnd = clone $tmpStart;
$tmpEnd = $tmpEnd->modify('last day of this month');
$tmpEnd = $tmpEnd < $end ? $tmpEnd : clone $end;
echo $tmpStart->format("M") . " " . ($tmpEnd->diff($tmpStart)->format("%a")+1) . "\n";
$tmpStart = $tmpEnd->modify('first day of next month');
}
This outputs:
Jan 31
Feb 28
Mar 31
Apr 30
May 15
get the number of days in month from cal_days_in_month() function and use date_diff() to compare between those
If I understand correctly, you want to find the difference in days between two dates?
<?php
$startDate = mktime(0, 0, 0, 1, 1, 1970); //Enter your start date and time
$endDate = mktime(0, 0, 0, 5, 20, 2014); //Enter your end date and time
echo ($endDate-$startDate)/60/60/24; //This works out the number of days between the two
?>
Outputs:
16210
Look at the mktime() documentation to understand how to input your date into the mktime() function.

Categories