I have a date, and need to add 24 Months (and not 2 Year) to it. This is what I tried:
strtotime("+24 months", $mydate);
If my date is, 20th Dec 2013 then the computed date is coming as 10th Dec 2015, whereas my expected date was 20th Dec 2015.
I know, what is going behind the scene:
2 Year: 365 days x 2 = 730 days
24 Months: 24 x 30days = 720 days
This gives me the missing 10 days. But how to over come this issue.
In Java we have Calendar class, which takes care of such calculations. However, I din't find anything here.
Can this issue be resolved.? Or I need to handle it manually?
You should always use the DateTime() class for anything like that.
i.e
$date = new DateTime("UTC");
//get date in 24months time:
$date->add(new DateInterval("P24M"));
//output date:
echo $date->format("d/m/Y H:i:s");
By using the DateTime and DateInterval classes, you can be sure that it will account of leap years and other such irregularities in dates.
See more at: http://php.net/manual/en/class.datetime.php
I hope this helps.
$mydate = "2014-10-01";
echo date('d-m-Y',strtotime("+24 months", strtotime($mydate)));
DateTime should work perfectly here:
$date = new DateTime("20th Dec 2013");
echo $date->format("d-m-Y");
$date->add(new DateInterval('P24M'));
echo $date->format("d-m-Y");
Output:
20-12-2013
20-12-2015
Demo
Sidenote:
I couldn't reproduce your error with:
echo date("d-m-Y", strtotime("+24 months", $mydate = strtotime("2013-12-20")));
See:
http://3v4l.org/HURAt
Related
I have this:
$previousMonth = new DateTime('2019-12-31');
$previousMonth->modify('-1 month');
My understanding is '-1 month' should modify the object regardless of number of days in that month, or?
Naturally what should I get or expect to get is end of Nov(2019-11-30) but what I get is first of December(the same month).
BTW if I change the date to '2019-12-30'(one day prior) then it will be end of Nov.
If my initial assumption is not correct, then what is the best alternative to reliably calculate the previous month?
Any thoughts?
The simplest and easiest way to get the last month in php is
$previousMonth = date("Y-n-j", strtotime("last day of previous month"));
Same as been suggested on other thread Getting last month's date in php
$date = "2019-12-31 00:00:00";
echo date('Y-m-d', strtotime($date . '-1month'));
This prints out 2019-12-01 as the 31/11 does not exist.
The following doesn't answer your question but may help in the future. I like to use Carbon when working with dates. Your issue could be resolved quite simply with this.
https://carbon.nesbot.com/
It has many functions and is extremely simple to use, and it can be installed with Composer.
To get the last day of the previous month, you can get the first day of the current month and substract 1 second or 1 day :
$previousMonth = new DateTime('2019-12-31');
$previousMonth->modify($previousMonth->format('Y-m-01')); // date is 2019-12-01 00:00:00
$previousMonth->modify('-1 sec');
echo $previousMonth->format('Y-m-d H:i:s') . PHP_EOL; // Outputs 2019-11-30 23:59:59
$previousMonth->modify('+1 sec'); // set back the original date 2019-12-01 00:00:00
$previousMonth->modify('-1 day');
echo $previousMonth->format('Y-m-d H:i:s'); // Outputs 2019-11-30 00:00:00
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
I can get the for example 19 March of specific date with this code:
$date = strtotime(" 19 March", $current_time);
For example if I gave the unix timestamp of 1st of January of 2010 as an input, It gave me 19 March of 2010. But also if I gave the unix timestamp of 20 March of 2010,I still get 19 March 2010. What I want is to get the next 19 March which in this case, It would be 19 March of 2011.
How can I do that?
Using PHP DateTime this can be achieved as follows:
// New DateTime object
$date = new DateTime('2010-03-19');
// Add a year
$date->add(new DateInterval('P1Y'));
// Output timestamp
echo $date->getTimestamp();
You can do something like as
$get = "19 March";
$given_date = "01 January 2010";
$date_month = date('d F',strtotime($given_date));
$year = date('Y',strtotime($given_date));
if(strtotime($given_date) - strtotime($date_month) < 0){
echo date('l,d F Y',strtotime("$get $year"));
}else{
echo date('l,d F Y',strtotime("$get ".($year+1)));
}
You should first get year from specified date. Then after you can create 19 march date with year and use strtotime() to get timestamp.
//add format according to your current_time variable format
$date = DateTime::createFromFormat("Y-m-d", $current_time);
echo $date->format("Y");
$fixed_date = strtotime($date->format("Y")."-03-19");
You can specify how many days or week you want to add or subtract from a day, as well as set the time with these functions
$nextUpdate = new DateTime("+5 day 1:00 pm");
echo $nextUpdate->getTimestamp();
$nextWeek = new DateTime("+1 week 9:00 am");
echo $nextWeek->getTimestamp();
This is a follow up to this question: Counting down days not showing the right number of days
I'm still confused about dates and times.
Setting the start and end times:
// start date: set the time of when you click the link
$startTime = strtotime('now');
$plantStart = date('M d, Y h:i:s', $startTime);
// end date: 3 days from the time of when you click the link
$date = strtotime("+3 day", $startTime);
$plantEnd = date('M d, Y h:i:s', $date);
This gives me:
Mar 17, 2014 07:33:45 (start)
Mar 20, 2014 07:33:45 (end)
Now, the problem.. when I do this:
// show how many days/hours till $plantEnd date
$d = new DateTime($plantEnd);
$daysHours = $d->diff(new DateTime())->format('%d Days, %H Hours');
echo $daysHours;
The result is always something like: 2 Days, 11 Hours its never 3 days 0 hours or 2 days 23 hours.. Is it still just getting the time till 0:00:00 on the 3rd day instead of to the exact minute of the time?
As Yohann Tilotti mention in the comments, the issue it that new DateTime() is never initialized in the diff() function.
What you probably meant was: $d->diff(new DateTime($plantStart)).
You can see a running example here: http://ideone.com/FU8akb
Like I commented, stick to one method of dates. The preferred method being DateTime:
$plantStart = new DateTime('now');
echo $plantStart->format('Y-m-d H:i:s');
// Output: 2014-03-17 12:56:00
$plantEnd = new DateTime('now + 3 days');
echo $plantEnd->format('Y-m-d H:i:s');
// Output: 2014-03-20 12:56:00
$daysHours = $plantEnd->diff(new DateTime())->format('%d Days, %H Hours');
echo $daysHours;
// Output: 3 Days, 00 Hours
I need to find a date after a certain number of weeks. For example, if the start date is Saturday, 31 October 2009, and if I choose 16 weeks then I need to find the date after sixteen Saturdays.
Thanks in advance.
You can use strtotime:
// Oct 31 2009 plus 16 weeks
echo date('Y-m-d', strtotime('2009-10-31 +16 week')); // outputs 2010-02-20
// next week
echo date('Y-m-d', strtotime('+1 week')); // outputs 2009-11-07
strtotime() is what you want: it takes an expression and turns it into a date object. Also see date():
$date = '31 october 2009';
$modification = '+ 16 weeks';
echo date('Y-m-d (l)', strtotime("$date")); //2009-10-31 (Saturday)
echo date('Y-m-d (l)', strtotime("$date $modification")); // 2010-02-20 (Saturday)
If you're using PHP 5.2.0 or later, you can use the DateTime class
<?php
$date = new DateTime('31 October 2009'); // This accepts any format that strtotime() does.
// Now add 16 weeks
$date->modify('+16 weeks');
// Now you can output it however you wish
echo $date->format('Y-m-d');
?>
Why not just use unix time, subtract 7 * 24 * 3600 * 16 from that and then convert that back into the date that you need.
This will help with the conversion back:
http://php.net/manual/en/function.date.php