PHP strtotime: Get previous month [duplicate] - php

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

Related

How can I get the last day of the month's timestamp? [duplicate]

This question already has answers here:
Timestamps of start and end of month
(7 answers)
Closed 8 years ago.
I have the current unix timestamp stored in variable $current_time. Using this variable, I want to find out exactly what the timestamp is for the very last day of the current month.
How can I do this in PHP 5.4+?
Here's a working example:
// this is if your $current_time is not within the current month (although the code below is valid for every single month and every single timestamp)
$month = date("M", $current_time);
$last_day_timestamp = strtotime('last day of ' . $month);
echo date("d-m-Y", $last_day_timestamp);
// if your $current_time is within the current month
$last_day_timestamp_of_this_month = strtotime('last day of this month')
Also you could use this as a reference for other relative date/time formats:
http://php.net/manual/en/datetime.formats.relative.php

getting the numeric value of "this Monday" - php

I'm trying to display a numeric value of the current week,
in a calendar-like feature that I have on a website I'm working on.
So, the function that I came up with in the end, working, is...
echo date("d", strtotime("this Tuesday"));
All goes well for most of the days,
except that, for example, today is Tuesday, the 25th, all week dates display well
since Tuesday to Sunday (25 - 30),
but for Monday it will say 01, because I think it thinks that "this Monday" is the 01 of December, not yesterday. This Monday usually should be the Monday of this week, yesterday (24), but the function when you apply "this Monday" will display the next Monday, because today it's already Tuesday, and yesterday is out of the scope ?
But this is weird, because i'm reffering to "this Monday", Monday of this week. It displays the next Monday. Is that a bug in the date function ?
How do I safely get the values of the days of the week otherwise?
thanks!
strtotime("Monday this week");
From the manual:
reltext space 'week' | Handles the special format "weekday + last/this/next week". | "Monday next week"
Try it with "last Monday" and "next Monday" keywords.
For Example:
echo date("d", strtotime("last Monday"));
another example...
echo date("d", strtotime("next Monday"));

Get date from the three and a half months ago

I know I can do
$threemonthsago = date("Y-m-d", strtotime("-3 month"));
echo "$threemonthsago";
However I want to get 3.5 months ago not three months ago. So I tried to do
$threemonthsago = date("Y-m-d", strtotime("-3.5 month"));
echo "$threemonthsago";
However it does not seem to give me the correct date its giving me like September something which it should not be since its currently April.
The decimal throws off strtotime() as that is not a valid format it recognizes. The real issue you have is what exactly is half of a month? If you traverse February it gets really dicey.
This is somewhat easier to do using DateTime() and DateInterval() if you specify exactly what half of a month is:
$date = new DateTime();
$new_date = $date->sub(new DateInterval('P3M15D'));
echo $new_date->format('Y-m-d');
Demo
echo(strtotime("-105 days"));

Get the following day after a month [duplicate]

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"));

PHP strtotime +1 month adding an extra month [duplicate]

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.

Categories