Add date in php gives different results - php

I have to get a date which is 6 months added with specific date.
I used the following code
$start_date = "2016-08-30";
$end_date= date( "Y-m-d", strtotime( "$start_date +6 months" ) );
echo $end_date;
which gave result as 2017-03-02
Then I changed the start date in code as below
$start_date = "2016-09-01";
$end_date= date( "Y-m-d", strtotime( "$start_date +6 months" ) );
echo $end_date;
which is giving result as 2017-03-01
Why is this happening at first place? Is there anything wrong with my code?
Using Mysql query
SELECT DATE_ADD('2016-08-30', INTERVAL 6 MONTH)
gives result 2017-02-28
Which is the right solution to get the correct date?

This happens due to PHP's behavior. In this case 6 months are added which gives february(it has 28 days) so it adds three more days , but in MySQL it adds only months.
To solve this use last day of 6 month or last day of sixth month instead of +6 months
Code
$date = new DateTime( '2016-08-30' );
echo $date->format( 'Y-m-d' ), "\n";
$date->modify( 'last day of sixth month' );
echo $date->format( 'Y-m-d' ), "\n";
Code Demo
Output
2016-08-30
2017-02-28
Another Solution
$date = new DateTime( '2016-08-25' );
echo $date->format('Y-m-d'),"\n";
$day = $date->format('j');
$date->modify('first day of sixth month');
$date->modify('+' . (min($day, $date->format('t')) - 1) . ' days');
echo $date->format('Y-m-d');
Code Demo
Output
2016-08-25
2017-02-25
Also refer Relative Formats

This function DOES NOT work from left-to-right as one would think. This function parses the string as a whole, then applies the intervals by size (year, month, ...). Take the following example:
<?php
$Date = strtotime('2011-02-22'); // February 22nd, 2011. 28 days in this month, 29 next year.
echo date('n/j/Y', strtotime('+1 year, +7 days', $Date)); // add 1 year and 7 days. prints 2/29/2012
echo "<br />";
echo date('n/j/Y', strtotime('+7 days, +1 year', $Date)); // add 7 days and 1 year, but this also prints 2/29/2012
echo "<br />";
echo date('n/j/Y', strtotime('+1 year', strtotime('+7 days', $Date))); // this prints 3/1/2012, what the 2nd would do if it was left-to-right
?>

Related

date half month not working

I tried to do like this
date('Y-m-d', strtotime('0.5 months'));
But it gives me date 1970-01-01
Expected date is 2017-10-15
So the question is why the half month is not working?
You can't use non-integers in a strtotime() relative date format:
number [+-]?[0-9]+
To make this work, you can use either 2 weeks or 15 days (note that these two return different days, since 2 weeks is 14 days):
<?php
echo date("Y-m-d", strtotime("2 weeks")).PHP_EOL; // 2017-10-15
echo date("Y-m-d", strtotime("15 days")).PHP_EOL; // 2017-10-16
echo date("Y-m-d", strtotime("+.5 months")).PHP_EOL; // not valid, returns 1970-01-01
Demo
I think you forgot the Plus singn
Please try with below
date('Y-m-d', strtotime('+ 0.5 months'));
or
date('Y-m-d', strtotime('+ 15 days'));
(If you convert in days)
hope will help you
I'm looking for a solution using only php relative time for a half month. The problem with the previous answers is that they don’t take into account the fluctuation in the amount of days per month. Consider this:
$time = strtotime('last day of feb 2019');
$time = strtotime('-2 weeks', $time);
$formatted_date = date('m/d', $time);
Output is 02/14 which is exactly in the middle of the month. But what about this:
$time = strtotime('last day of jan');
$time = strtotime('-2 weeks', $time);
$formatted_date = date('m/d', $time);
Output is 01/17, which is 1-2 days off from the true middle of the month (01/15.5) depending on if you round up or down.
Also, per the answer with strtotime('+0.5 months'), I know this was addressed, but this is an excerpt straight from the PHP docs:
number is an integer number; if a decimal number is given, the dot (or comma) is likely interpreted as delimiter. For instance, '+1.5 hours' is parsed like '+1 5 hours', not as '+1 hour +30 minutes'.
I’ve experimented with quite a few phrasings to get to the true middle of the month, and I’ve come to the conclusion that it isn’t possible with relative formats alone. So here is my solution:
<?php
function middleOfMonth( $time ) {
$n = cal_days_in_month( CAL_GREGORIAN, date('n', $time), date('Y', $time) );
return $n / 2;
}
$time = strtotime('jan next year');
$middle = middleOfMonth( $time ); // result is 15.5
echo( ceil( $middle ) ); // outputs 16
echo( floor( $middle ) ); // outputs 15

PHP add month to date on a date with day of month 31

I'm using code that works fine on all dates, except 2015-05-31.
The code brings me the first day of next month.
it works on every date, even if day of month is 31.
$time = strtotime('2015-07-31');
$final = date("Y-m-1", strtotime("+1 month", $time));
echo $final;
output will be --> 2015-08-1.
For some reason on the date 2015-05-31 it returns 2015-07-1 instead of 2015-06-01
$time = strtotime('2015-05-31');
$final = date("Y-m-1", strtotime("+1 month", $time));
echo $final;
Its probably because 6-2014 has 30 days, and 8-2014 has 31 days, so +1 month adds 30 days and not a "month".
How could i get correctly the first day of next month on every date?
Thank you.
I think this should work -
$time = strtotime("2015-05-31");
echo $final = date("Y-m-d", strtotime("first day of next month", $time));
You can try like this:
<?php
$date = new DateTime('2000-12-31');
$interval = new DateInterval('P1M');
$date->add($interval);
echo $date->format('Y-m-d') . "\n";
$date->add($interval);
echo $date->format('Y-m-d') . "\n";
?>
Output:
2001-01-31
2001-03-03
For more information :http://in3.php.net/manual/en/datetime.add.php

First Date of Each Week in a Month

I am having a small issue where I have the month (December 2013) and the Week Number (1, 2, 3 etc) and I need to find out the first date of each week.
I cannot do a simple search like finding the closest Sunday relative to a date since some weeks don't start on a Sunday at all.
Any help would be appreciated.
Considering you have the date of 1st of any month :
First find the next Sunday
Then add 1 week at a time to it to get subsequent Sunday's
Code :
$firstDayOfMonth = '2013-12-01'; // Try also with first day of other months
$week1 = $firstDayOfMonth;
$week2 = date( "Y-m-d" ,strtotime('next Sunday', strtotime( $week1 ) ) );
$week3 = date( "Y-m-d" ,strtotime('+1 week', strtotime( $week2 ) ) );
$week4 = date( "Y-m-d" ,strtotime('+1 week', strtotime( $week3 ) ) );
$week5 = date( "Y-m-d" ,strtotime('+1 week', strtotime( $week4 ) ) );
echo '
Week 1 starts on : ' .$week1 .'<br>
Week 2 starts on : ' .$week2 .'<br>
Week 3 starts on : ' .$week3 .'<br>
Week 4 starts on : ' .$week4 .'<br>
Week 5 starts on : ' .$week5 .'<br>';
Output :
Week 1 starts on : 2013-12-01
Week 2 starts on : 2013-12-08
Week 3 starts on : 2013-12-15
Week 4 starts on : 2013-12-22
Week 5 starts on : 2013-12-29
Just try
<?php
$first_day_this_month = date('M-D-Y',strtotime(date('M-01-Y')));
echo date('M-D-Y',strtotime(date('M-01-Y')))." = ".date('M-d-Y',strtotime(date('M-01-Y')));//First day of month
echo "<br>";
$last_day_this_month = date('t');//last day of month
$last_day_num=intval($last_day_this_month);
for($i=1;$i<$last_day_num;$i++)
{
$first_day_this_month = date('M-D-Y',strtotime(date('M-'.$i.'-Y')));//All day of month
$dateopt_arr=date('M-N-Y',strtotime(date('M-'.$i.'-Y')));
$dateo=explode('-',$dateopt_arr);
//echo $dateo[1];
if(($dateo[1]%7)==0)
{
echo $first_day_this_month = date('M-D-Y',strtotime(date('M-'.$i.'-Y')))." = ".date('M-d-Y',strtotime(date('M-'.$i.'-Y')));//Week day of month
echo "<br>";
}
}
?>
Use the second argument of strtotime to specify the date to calculate "next Sunday" and other relative dates from:
strtotime('next Sunday', strtotime('09/12/2013'));
http://us2.php.net/strtotime
Example:
echo date('m/d/Y', strtotime('next Sunday', strtotime('09/12/2013')));

Calculating week start/end dates for the next X weeks

I am trying to build a function in PHP that, depending on the date will give me
The current week (mon-sun) 20/8/12 to 26/8/12
Following week+1 (mon-sun) 27/8/12 to 02/9/12
Following week+2 (mon-sun) 03/9/12 to 09/9/12
Following week+3 (mon-sun)
Following week+4 (mon-sun)
Following week+5 (mon-sun)
I have tried using the following, but is there anything cleaner??
$week0_mon = date("Y-m-d", strtotime(date("Y").'W'.date('W')."1"));
$week0_sun = date("Y-m-d", strtotime(date("Y").'W'.date('W')."7"));
$week1_mon = date("Y-m-d", strtotime(date("Y-m-d", strtotime($week0_mon)) . " +1 week"));
$week1_sun = date("Y-m-d", strtotime(date("Y-m-d", strtotime($week0_sun)) . " +1 week"));
echo $week0_mon.' to '.$week0_sun.'<br />';
echo $week1_mon.' to '.$week1_sun.'<br />';
Maybe this will answer to your issue, it calculates the previous monday and start from here to add one week at a time. Just edit the for
$dOffsets = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$prevMonday = mktime(0,0,0, date("m"), date("d")-array_search(date("l"),$dOffsets), date("Y"));
$oneWeek = 3600*24*7;$toSunday = 3600*24*6;
for ($i=0;$i<= 5;$i++)
{
echo "Week +",$i," (mon-sun) ",
date("d/m/Y",$prevMonday + $oneWeek*$i)," to ",
date("d/m/Y",$prevMonday + $oneWeek*$i + $toSunday),"<br>";
}
This gives me
Week +0 (mon-sun) 20/08/2012 to 26/08/2012
Week +1 (mon-sun) 27/08/2012 to 02/09/2012
Week +2 (mon-sun) 03/09/2012 to 09/09/2012
Week +3 (mon-sun) 10/09/2012 to 16/09/2012
Week +4 (mon-sun) 17/09/2012 to 23/09/2012
Week +5 (mon-sun) 24/09/2012 to 30/09/2012
I've adjusted #Wr1t3r answer to give correct date ranges as follows:
function plus_week($addWeek=0){
$last_monday_timestamp=strtotime('-'.(date('N')-1).' days');
if($addWeek!=0){
if($addWeek>0) $addWeek='+'.$addWeek;
$last_monday_timestamp=strtotime($addWeek.' week', $last_monday_timestamp);
}
$end_week_timestamp = strtotime ('+6 days', $last_monday_timestamp);
return date('d/m/y', $last_monday_timestamp).' to '.date('d/m/y', $end_week_timestamp);
}
date('N') will give weekday number mon-sun (1-7) so if we subtract 1 from this we know how many days to go back to last monday. OR we could have used strtotime('last monday').
BUT this way ensures we dont go back to last monday if we are currently on monday.
Monday=1 so (1-1=0) -0 days = today
Friday=5 so (5-1=4) -4 days = monday
SUNDAY=7 (not 0 if we used 'w') so (7-1=6) -6 days = monday (not tomorrow)
I've also adjusted this to do negative week numbers too.
I did it like this. I'm not sure if it is exactly what you want.
function plus_week($addWeek){
$date = date("d.m.Y",time());
$newdate = strtotime ( '+'.$addWeek.' week' , strtotime ( $date ) ) ;
$newdate = date ( 'd/m/y' , $newdate );
return $newdate;
}
for($i = 1; $i < 7; $i++){
echo "Following week+".$i." ".plus_week($i)." to ".plus_week($i+1)."<br/>";
}
From this you will get answer like this:
Following week+1 29/08/12 to 05/09/12
Following week+2 05/09/12 to 12/09/12
Following week+3 12/09/12 to 19/09/12
Following week+4 19/09/12 to 26/09/12
Following week+5 26/09/12 to 03/10/12
Following week+6 03/10/12 to 10/10/12

php - find date for same day of week for last year

So, in PHP i'm trying to return a date value for last year based on the same day of week.
EX: (Monday) 2011-12-19 inputted should return (Monday) 2010-12-20.
I was just doing it simply by -364 but then that was failing on leap years. I came across another function :
$newDate = $_POST['date'];
$newDate = strtotime($newDate);
$oldDate = strtotime('-1 year',$newDate);
$newDayOfWeek = date('w',$oldDate);
$oldDayOfWeek = date('w',$newDate);
$dayDiff = $oldDayOfWeek-$newDayOfWeek;
$oldDate = strtotime("$dayDiff days",$oldDate);
echo 'LAST YEAR DAY OF WEEK DATE = ' . date('Ymd', $oldDate);
however, that is failing when you try to input a Sunday date, as it does a 0 (sunday) minus 6 (saturday of last year date), and returns with a value T-6. IE inputting 2011-12-25 gets you 2010-12-19 instead of 2011-12-26.
I'm kind of stumped to find a good solution in php that will work for leap years and obviously all days of the week.
Any suggestions?
Thanks!
How about this, using PHP's DateTime functionality:
$date = new DateTime('2011-12-25'); // make a new DateTime instance with the starting date
$day = $date->format('l'); // get the name of the day we want
$date->sub(new DateInterval('P1Y')); // go back a year
$date->modify('next ' . $day); // from this point, go to the next $day
echo $date->format('Ymd'), "\n"; // ouput the date
$newDate = '2011-12-19';
date_default_timezone_set('UTC');
$newDate = strtotime($newDate);
$oldDate = strtotime('last year', $newDate);
$oldDate = strtotime(date('l', $newDate), $oldDate);
$dateFormat = 'Y-m-d l w W';
echo "This date: ", date($dateFormat, $newDate), "\n";
echo "Old date : ", date($dateFormat, $oldDate);
That gives:
This date: 2011-12-19 Monday 1 51
Old date : 2010-12-20 Monday 1 51
Use strtotime() to get a date, for the same week last year.
Use the format {$year}-W{$week}-{$weekday}, like this:
echo date("Y-m-d", strtotime("2010-W12-1"));
And you can do that for as long back you wan't:
<?php
for($i = 2011; $i > 2000; $i--)
echo date("Y-m-d", strtotime($i."-W12-1"));
?>
Make it easier :)
echo date('Y-m-d (l, W)').<br/>;
echo date('Y-m-d (l, W)', strtotime("-52 week"));
Edit: I forgot to write output: :)
2015-05-06 (Wednesday, 19)
2014-05-07 (Wednesday, 19)
<?php
$date = "2020-01-11";
$newdate = date("Y-m-d",strtotime ( '-1 year' , strtotime ( $date ) )) ;
echo $newdate;
?>
ref https://www.nicesnippets.com/blog/how-to-get-previous-year-from-date-in-php

Categories