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
Related
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
This question already has answers here:
PHP subtract 1 month from date formatted with date ('m-Y')
(11 answers)
Closed 5 years ago.
How do i properly minus 1 month for the current month ?
$current_month1 = date('m');
$current_month = $current_month1-1;
echo $current_month;
//current ouput
6
//desired output
06
check the following:
$now = new \DateTime("now");
$past = $now->modify("-1 month");
DateTime::modify docs
Also you can do it using DateInterval, the docs has example.
You can use date in combination with strtotime for this.
echo date('m', strtotime('last month')); // 06
The m operator in date will get you:
echo date('m', strtotime('now - 1 month'));
Gives 06.
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......
This question already has answers here:
Get which day of the week the month starts on for given input
(2 answers)
Closed 8 years ago.
I've been trying to get the code below to work, and while it worked with June it is not working with July.
The below results in a value of 3 for $first_day_of_month when it should be 2 for Tuesday.
$date = strtotime('20140702'); // July 02, 2014
$month = date('m',$date);
$year = date('Y',$date);
$days_in_month = date('t',$date);
$first_day_of_month = date('w', strtotime($year . $month . 01)); // sunday = 0, saturday = 6
The strtotime function supports relative time formats. You can just do this instead:
$date = strtotime('20140702');
$first_date = strtotime('first day of this month', $date);
$first_day_of_month = date('w', $first_date);
The second parameter to strtotime provides the time that the relative format will be relative to. You can use this to easily calculate dates relative to a particular point, as shown above.
You would have to cast 01 to a string or else php will compute the time for 2014071 rather 20140701
strtotime($year . $month . '01')
You should look at mktime().
In your case your best approach would be:
$date = strtotime('20140702'); // July 02, 2014
$month = date('m',$date);
$year = date('Y',$date);
$days_in_month = date('t',$date);
$first_day_of_month = date('w', mktime(0,0,0,$month,1,$year)); // sunday = 0, saturday = 6
As a bonus you can also get the last day of the month
$last_date_of_month = mktime(0,0,0,$month+1,0,$year);
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.