This question already has answers here:
PHP strtotime +1 month adding an extra month [duplicate]
(7 answers)
How can I get a date after 15 days/1 month in PHP?
(6 answers)
Closed 9 years ago.
Just a noob question,
For example I have date 2013-08-01 // 1 August 2013
How do I get same day next month. ex. 2013-09-01 // 1 Sept 2013
Thanks
I'm not a PHP dev, but it seems to me you want DateTime::add
$date->add(new DateInterval('P1M'));
As noted in the documentation though, you need to be careful near the end of a month - adding 1 month to January 31st 2001 ends up with March 3rd, for example. (Not the behaviour I'd choose, but that's a different matter.) If you're always dealing with the 1st of the month, you should be okay.
With using strtotime
echo date("Y-m-d",strtotime('next month',strtotime('2013-08-01')));
You can use the date_add function (procedural) or Datetime::add (OO). Here is an example of each:
$date = new DateTime('2000-01-01');
$date->add(new DateInterval::createFromDateString('1 month'));
or
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('1 month'));
Try this:
$dateTest = date('Y-m-d', strtotime("$dateTest + 1 month"));
Related
This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 3 years ago.
Without knowing or using any kind of date, what's the best way to just add 1 day to the day of the week?
For example, I have Monday and I want to add +1 Days. Something like this
echo ('Monday + 1 days');
I tried with date(); but couldn't find a solution. Any ideas?
You can simply do:
<?php
echo date('l', strtotime('Monday + 1 day'));
We just use the strtotime logic of adding a time period and wrap it in a date format of l - which is full text representation of the day.
l (lowercase 'L') A full textual representation of the day of the week Sunday through Saturday
refs:
https://secure.php.net/manual/en/function.strtotime.php
https://secure.php.net/manual/en/function.date.php
There is no built-in function in PHP that says "the day after Monday is Tuesday" without referring to a particular Monday (which is what strtotime would do). Of course, you may choose to not care that they refer to a particular Monday and Tuesday, since the pattern hasn't changed recently and will not change in the foreseeable future.
You can use an if statement or a switch-case statement. You can even make it work in a different language.
if ($date === "lundi") return "mardi";
if ($date === "mardi") return "mercredi";
// etc
This question already has answers here:
In PHP, is there an easy way to get the first and last date of a month?
(12 answers)
Closed 9 years ago.
I have date strings in "Y-m" format. Day is irrelevant here. Now I need to create another date from these date strings wheere day should be either 1st or the last day of the month. I have tried this code:
$curDate = DateTime::createFromFormat('Y-m', $datestring1);
$dt = $curDate->format('Y-m-d');
The problem here is that it creates dates with the current day of the months. How can I force it to do it for the 1st or last day?
Thanks
first day:
$dt = $curDate->format('Y-m-01');
last day:
$dt = $curDate->format('Y-m-t');
t: Number of days in the given month
http://php.net/manual/en/function.date.php
You can specify hard code value
$curDate = DateTime::createFromFormat('Y-m', $datestring1);
$dt = $curDate->format('Y-m-1'); //if you want first day
$dt = $curDate->format('Y-m-t'); //if you want last day
This question already has answers here:
PHP: Adding months to a date, while not exceeding the last day of the month
(7 answers)
Closed 9 years ago.
I have a simple variable that adds one month to today:
$endOfCycle = date("Y-m", strtotime("+1 month"));
Today is January 2013, so I would expect to get back 2013-02 but I'm getting 2013-03 instead. I can't figure out why it's jumping to March.
It's jumping to March because today is 29th Jan, and adding a month gives 29th Feb, which doesn't exist, so it's moving to the next valid date.
This will happen on the 31st of a lot of months as well, but is obviously more noticable in the case of January to Feburary because Feb is shorter.
If you're not interested in the day of month and just want it to give the next month, you should specify the input date as the first of the current month. This will always give you the correct answer if you add a month.
For the same reason, if you want to always get the last day of the next month, you should start by calculating the first of the month after the one you want, and subtracting a day.
This should be
$endOfCycle=date('Y-m-d', strtotime("+30 days"));
strtotime
expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.
while
date
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
See the manual pages for:
http://www.php.net/manual/en/function.strtotime.php
http://www.php.net/manual/en/function.date.php
You can use this code to get the next month:
$ts = mktime(0, 0, 0, date("n") + 1, 1);
echo date("Y-m-d H:i:s", $ts);
echo date("n", $ts);
Assuming today is 2013-01-31 01:23:45 the above will return:
2013-02-01 00:00:00
2
today is 29th of January, +1 month means 29th of Fabruary, but because February consists of 28 days this year, it overlaps to the next day which is March 1st
instead try
strtotime('next month')
Maybe because its 2013-01-29 so +1 month would be 2013-02-29 which doesn't exist so it would be 2013-03-01
You could try
date('m/d/y h:i a',(strtotime('next month',strtotime(date('m/01/y')))));
from the comments on http://php.net/manual/en/function.strtotime.php
$endOfCycle = date("Y-m", mktime(0, 0, 0, date("m", time())+1 , 15, date("m", time())));
try this:
$endOfCycle = date("Y-m", time()+2592000);
this adds 30 days, not exactly a month tough.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get previous month and year relative to today, using strtotime and date?
Today is December, 31st but strtotime("-1 months") returns December:
echo date("Y-m", strtotime("-1 months"));
Same for strtotime("last month")
How can I properly return the previous month (November)?
Test: http://codepad.viper-7.com/XvMaMB
strtotime("first day of last month")
The first day of is the important part as detailed on the Relative Formats manual page.
Example: http://codepad.viper-7.com/dB35q8 (with hard-coded today's date)
strtotime("-1 months") would be 2012-11-31, but there's no November, 31st. It is one day past 2012-11-30, which gives 2012-12-01. You will see it, when you do
echo date("Y-m-d", strtotime("-1 months"));
gives as output
2012-12-01
See codepad
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
Let's say your creating an application that needs to send you an email monthly. The date start date is FEB 29, 2012. How do you deal with that for next year since there is no FEB 29, 2013. Likewise if the start date is JAN 31, there is no FEB 31 or APR 31, and so on...
Simply use the DateTime class which is perfect for this:
If I want the date 1 month from 29/2/2012:
$date = new DateTime('2012-02-29'); // "start date"
$date->add(new DateInterval('P1M')); //one month later
echo $date->format('Y-m-d') . "\n"; //2012-03-29
And a year later:
$date = new DateTime('2012-02-29'); //start
$date->add(new DateInterval('P1Y')); //one year later
echo $date->format('Y-m-d') . "\n"; //2013-03-01
Using this, you can easily caculate any date intervals you require.
Send it on the 1st of the month instead :)
I would test the date before saving it in the database.
Get last day of any other month
Enter any month/day/year to get the last day of that month
$lastday = date('t',strtotime('3/1/2009'));
If you want to display the last day of this month on your website, do the following:
<?php
echo date('t',strtotime('today'));
?>
Source: http://www.johnboy.com/blog/find-the-last-day-of-the-month-with-php
When the script runs check the actual date if its the last day of the month and act according to the result.