Need to find future date using PHP - php

I need to find future date using php.But using php, I am unable to get correct date for February.I posted my code below.I need end date of February.Thanks in advance.
<?php
$date = '2011-12-31';
$dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($date)) . " +2 month");
$end_date=date("Y-m-d",$dateOneMonthAdded);
echo date("Y-m-d",strtotime(date("Y-m-d", strtotime($end_date))));
?>

You can use the 1st of March and subtract 1 day:
$date = date_create('2012-03-01');
$date->modify("-1 day");
echo $date->format("Y-m-d");

Try this code.
<?php
$date = '2011-12-31';
$dateOneMonthAdded = strtotime(date("Y-m-t", strtotime($date)) . " +2 month");
$end_date=date("Y-m-t",$dateOneMonthAdded);
echo date("Y-m-d",strtotime(date("Y-m-d", strtotime($end_date))));
?>

If you absolutely need to use strtotime, you could try with this:
<?php
$date = '2011-12-31';
$dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($date)) . ", next month, last day of next month");
$end_date=date("Y-m-d",$dateOneMonthAdded);
echo date("Y-m-d",strtotime(date("Y-m-d", strtotime($end_date))));
?>
Which will return what you expect: 2012-02-29.
You can take a look at relative formats for strtotime in the PHP Manual as well.

$dt = new DateTime();
$dt->setDate(2011, 12, 31);
$dt->modify('last day of +2 month');
//or
$dt->modify('+2 month -2 day');
//or
$dt->modify('next month last day of next month');
print_r ($dt);

use the below code to find the first/last day of a given month,
<?php
function findFirstAndLastDay($anyDate)
{
//$anyDate = '2009-08-25'; // date format should be yyyy-mm-dd
list($yr,$mn,$dt) = split('-',$anyDate); // separate year, month and date
$timeStamp = mktime(0,0,0,$mn,1,$yr); //Create time stamp of the first day from the give date.
$firstDay = date('D',$timeStamp); //get first day of the given month
list($y,$m,$t) = split('-',date('Y-m-t',$timeStamp)); //Find the last date of the month and separating it
$lastDayTimeStamp = mktime(0,0,0,$m,$t,$y);//create time stamp of the last date of the give month
$lastDay = date('D',$lastDayTimeStamp);// Find last day of the month
$arrDay = array("$firstDay","$lastDay"); // return the result in an array format.
return $arrDay;
}
//Usage
$dayArray=array();
$dayArray=findFirstAndLastDay('2009-02-25');
print $dayArray[0];
print $dayArray[1];
?>

<?php
$date = '2011-12-31';
$tmp_date = strtotime(date("Y-m-1", strtotime($date)) . " +2 month");
$end_date=date("Y-m-t",$tmp_date);
echo date("Y-m-d",strtotime(date("Y-m-d", strtotime($end_date))));
?>

If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$date = '2011-12-31';
$end_date = new DateTime($date);
$end_date->modify('last day of +2 month');
echo $end_date->format('Y-m-d');
Live Demo
OR
Use strtotime()
$date = '2011-12-31';
$dateOneMonthAdded = strtotime(date("Y-m-d", strtotime($date)) . ", last day of +2 month");
echo $end_date = date("Y-m-d",$dateOneMonthAdded);

Related

strtotime('-1 month') returning wrong date if month have 31 days

I'm trying make a function to return the exact date of previous months.
That is a example of my code:
// Dates in TimeStamp
$ts_now = strtotime('now');
$ts_month1 = strtotime("-1 month", $ts_now);
$ts_month2 = strtotime("-2 month", $ts_now);
$ts_month3 = strtotime("-3 month", $ts_now);
// Dates Formated
$date_now = date('Y-m-d', $ts_now);
$date_month1 = date('Y-m-d', $ts_month1);
$date_month2 = date('Y-m-d', $ts_month2);
$date_month3 = date('Y-m-d', $ts_month3);
//Output
echo $date_now; //2020-04-30
echo $date_month1; //2020-03-30
echo $date_month2; //2020-03-01
echo $date_month3; //2020-01-30
The problem is in $date_month2 that represents February, the output is 2020-03-01 instead 2020-02-29 and I suppose that problem will happen in months who have 30 days when present date have 31 days.
What is the best way to resolve that?
As you can see working with the end of the month can be problematic because of how PHP works with dates. Your best bet is to go back to the beginning of the month, do your date math (i.e. go backwards in time), and then go to the date you want. That way you can check to see if the current day is greater than the number of days in month. If so, use the last day of the month instead.
function getMonthsAgo(int $n): string {
$date = new DateTime();
$day = $date->format('j');
$date->modify('first day of this month')->modify('-' . $n . ' months');
if ($day > $date->format('t')) {
$day = $date->format('t');
}
$date->setDate($date->format('Y'), $date->format('m'), $day);
return $date->format('Y-m-d');
}
// Dates Formated
$date_now = date('Y-m-d');
$date_month1 = getMonthsAgo(1);
$date_month2 = getMonthsAgo(2);
$date_month3 = getMonthsAgo(3);
//Output
echo $date_now;
echo $date_month1;
echo $date_month2;
echo $date_month3;
Output:
2020-04-30
2020-03-30
2020-02-29
2020-01-30

Find date of after 2 months using php

When i have current date is in "31/12/2017". I need to find date of after 2 months that means its February. When its february i need to get as "29/2/2018". But When we use below code i got "03/03/2018". Can you please help me to solve this task,
Here i added my PHP code,
$xmasDay = new DateTime('2017-12-31 + 2 month');
echo $xmasDay->format('Y-m-d');
please try this
<?php
function add_month($date_str, $months)
{
$date = new DateTime($date_str);
// We extract the day of the month as $start_day
$start_day = $date->format('j');
// We add 1 month to the given date
$date->modify("+{$months} month");
// We extract the day of the month again so we can compare
$end_day = $date->format('j');
if ($start_day != $end_day)
{
// The day of the month isn't the same anymore, so we correct the date
$date->modify('last day of last month');
}
return $date->format('Y-m-d');
}
$result = add_month('2017-12-31', 2);
echo $result
calculate from first of the month, then use date t
$orginal = '2017-12-31';
$orginal = explode('-', $orginal);
$originalDate = strtotime($orginal[0] . '-' . $orginal[1] . '-01 00:00:00');
$newDate = strtotime('+2 month', $originalDate);
echo date('Y-m-t', $newDate);
// or
$date = new DateTime();
$date->setTimestamp($newDate);
echo $date->format('Y-m-t');

How to get starting date from week number

Getting week number this format :
$weeknumber = date('W', strtotime('10-02-2015'));
How to get starting date from this weeknumber.
you can use this
echo $weeknumber = date('W', strtotime('18-10-2012'));
$gendate = new DateTime();
$gendate->setISODate(2015,$weeknumber,4); //year , week num , day
echo $gendate->format('d-m-Y'); //"prints"
Use this
you need to change like this strtotime('10-02-2015'))
$date_string = "2012-10-18";
$weeknumber = date("W", strtotime($date_string));
//get the date from week number
isodate
$gendate = new DateTime();
$gendate->setISODate(2013,$weeknumber ,4); //year , week num , day
echo $gendate->format('d-m-Y'); //"prints" 26-12-2013
Don't mix strtotime() and date() functions with DateTime class. It can be simply done without them, and with only DateTime, like:
$date = new DateTime('10-02-2015');
$date->setISODate($date->format('o'), $date->format('W'));
echo $date->format('d-m-Y');
demo

How to get the current date in PHP, and add 1 month to the current date?

I'm coding a script where I require to save the current date, and the date 1 month from that date. I am pretty sure that the time() variable works, but I am not sure how to +1 month onto that?
Any ideas, suggestions. Cheers!
Try this
$today = date("Y-m-d");
$date = date('Y-m-d', strtotime('+1 month', $today));
or use DateTime()
$dt1 = new DateTime();
$today = $dt1->format("Y-m-d");
$dt2 = new DateTime("+1 month");
$date = $dt2->format("Y-m-d");
$time = strtotime("2010-12-11");
$final = date("Y-m-d", strtotime("+1 month", $time));
(OR)
strtotime( "+1 month", strtotime( $time ) );
this returns a timestamp that can be used with the date function
Use this:
Current Date:
echo "Today is " . date("Y/m/d");
1 Month to the Current Date:
$time = strtotime(date("Y/m/d"));
$final = date("Y-m-d", strtotime("+1 month", $time));
<?php
$current_time = date("Y-M-d h:i:s",time()); // Getting Current Date & Time
print $current_time; // Current Date & Time Printing for display purpose
$future_timestamp = strtotime("+1 month"); // Getting timestamp of 1 month from now
$final_future = date("Y-M-d h:i:s",+$future_timestamp); // Getting Future Date & Time of 1 month from now
print $final_future; // Printing Future time for display purpose
?>
shorter : $today=date("Y-m-d"); $date=
This one liner worked for me:
$monthFromToday = date("Y-m-d", strtotime("+1 month", strtotime(date("Y/m/d"))));
The given answers may not give you the results you might expect or desire.
Consider:
$today = "29Jan2018";
$nextMonth = date('dMY', strtotime('+1 month', (strtotime($today))));
echo $today // yields 29Jan2018
echo $nextMonth // yields 01Mar2018
$today = date("Y-m-d");
$enddate = date('Y-m-01',strtotime($today. ' + 1 months'));
You could also consider using the Carbon package.
The solution would look like this:
use Carbon\Carbon
$now = Carbon::now;
$now->addMonth();
Here is the link for reference https://carbon.nesbot.com/docs/

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