I'm looking for a reliable way to return the full date of a specified weekday (e.g. "Mon") for the current week.
Since today is Wednesday, June 13, 2012, I expected <?php echo date("Y-m-d", strtotime('Mon this week')); ?> to result in 2012-06-11, but instead php returns 2012-06-18 as though it interprets this week as meaning next week. Why this behavior and what should I be doing?
Thanks.
--Jeff
date( 'Y-m-d', strtotime( 'last Monday', strtotime( 'Sunday' ) ) );
This searches for the Monday previous to the next Sunday.
According to the documentation php relative date formats.
Then Monday this week would first advance to the next Monday and then process the relative text of this week.
dayname: Moves to the next day of this name unless it is the current day then it will not advance. In other words if the current date was June 11, then strtotime('Monday this week') would return June 11 whereas if the current date was June 13 then strtotime('Monday this week') would return June 19.
i think this is the solution for your problem:
$monday_date = date("Y-m-d", mktime(0,0,0, date("m"), date("j")-(date("w")+1), date("Y")));
Related
Is there an easy way to get last week's, say, Monday? If today is Tuesday, I do not want yesterday's Monday. Rather, I want the Monday 8 days ago (last week's Monday). Then I want that Monday's proceeding Sunday. Basically, I'm trying to get the date range for last week, Monday to Sunday.
This doesn't always work right:
date('Y-m-d', strtotime('last Monday')
Suggestions?
You can use “this week” format:
$monday = strtotime( 'this week', strtotime( '7 days ago' ) );
$sunday = strtotime( '+ 6 days', $monday );
3v4l.org demo
“this_week” returns monday of previous week, then — adding 6 days — you obtain the monday of relative week.
The strtotime function accepts the current date as a parameter.
http://php.net/manual/en/function.strtotime.php
Just pass in strtotime('last Sunday') as the parameter to get a weekday of the last full week.
$beginning_of_week = strtotime('last Sunday');
$result = date('Y-m-d', strtotime('last Monday', $beginning_of_week));
echo $result;
Currently, I have the timezone set to America/New York.
I have a timestamp of 1448933400, thats Monday, November 30, 2015 - 08:30 PM. I would like to get a relative time from this current timestamp as the "First Sun of Next Month".
So I am doing the following code:
strtotime("First Sun of Next Month", 1448933400);
The problem I am having is, is the returned value of 1449378000.
This is not at 8:30 PM, but instead at 12:00 AM. Is there an easy way to get relative dates to retain the relative time of the source timestamp?
There are many possible answers, but what you can do is first calculate the time offset for the day of your timestamp and apply it to the result:
<?php
$stamp = 1448933400;
$offset = $stamp - strtotime(date("Y-m-d", $stamp));
var_dump(strtotime("First Sun of Next Month", $stamp) + $offset);
?>
Output
int(1449451800) // or "2015-12-06 20:30:00"
I have a script that gets the current and last month in PHP like so:
$currentMonth = date('m');
//Expected:07
//Result:07
$lastMonth = date('m', strtotime('-1 months'));
//Expected:06
//Result:07
Today happens to be the 31 or end of the month of July. Is this result to be expected from PHP?
When using -31 days the result is as expected:
$lastMonth = date('m', strtotime('-31 days'));
//Expected:06
//Result:06
Here's a cleaner test case that doesn't expire:
<?php
$origin = mktime(18, 0, 0, 7, 31, 2015);
var_dump( date('r', $origin), date('r', strtotime('-1 months', $origin)) );
string(31) "Fri, 31 Jul 2015 18:00:00 +0200"
string(31) "Wed, 01 Jul 2015 18:00:00 +0200"
I'm pretty sure it's a documentation issue, because the manual clearly states this (emphasis mine):
Relative month values are calculated based on the length of months
that they pass through. An example would be "+2 month 2011-11-30",
which would produce "2012-01-30". This is due to November being 30
days in length, and December being 31 days in length, producing a
total of 61 days.
... and it's wrong.
PHP bug tracker has tons of dupes about this. They're all closed as not a bug. Here's a relevant comment from 2009 that explains it:
I agree that this is an annoying behaviour.
Also, the implementation is problematic. Basically if you use '+1
month' it takes the month number, adds 1 and parses result as a new
date.
If you use '+1 month' on the first of the month, it sets the date to
the next first of the month.
This behaviour gives the impression, that php considers the length of
a month, which is not true.
But if you use '+1 month' on the last day of a month, the result is
unexpected as 2009-05-31 becomes 2009-06-31 which is an invalid date
and then interpreted as 2009-07-01.
This should at least be mentioned in the documentation.
You can do this way
$d = new DateTime();
$currentMonth = $d->format('m');
//Expected:07
//Result:07
print $currentMonth;
$d->modify('first day of previous month');
print "<br/>";
$lastMonth = $d->format('m');
//Expected:06
//Result:06
print $lastMonth;
DEMO: http://codepad.viper-7.com/kokWi8
This is a issue with PHP's date-string parser. See here:
http://derickrethans.nl/obtaining-the-next-month-in-php.html
#Mr. Llama made a script showing what other dates this issue effects:http://codepad.viper-7.com/E4gP0W
The solution I went with:
//Date:07/31/15
$currentMonth = date('m');
//Result:07
$lastMonth = date('m', strtotime('first day of -1 months'));
//Result:06
-1 month is interpreted as "same day of month, last month". If this day does not exist, the date overflows into the next month. Actually the result is the same as strtotime("31.6.2015") - try it!
There is "s" in excess in month. It should be like this:
$lastMonth = date('m', strtotime('-1 month'));
I expected this functional to return 6/30/2005 instead of 7/1/2005.
print date("m/d/Y", strtotime("12/31/2004 +6 month"));
Similarly, print date("m/d/Y", strtotime("1/31/2011 +1 month")) returns 03/03/2011 while would like it to return 2/28/2011.
Does anyone know if there is a straight forward way to show the last day of the added month?
How about this?
echo date("m/d/Y", strtotime("last day of 12/31/2004 + 6 month")); // 6/30/2005
echo date("m/d/Y", strtotime("last day of 1/31/2011 + 1 month")); // 2/28/2011
Demo
Edit: For your reference, here is a link to the documentation for relative times.
as strtotime continue in to next month if there isn't enoghe days that month,
you can back 6 month and check if its end up on the start date
$date2 = date("Y-m-d", strtotime("{$date} +6 months"));
$date3 = date("Y-m-d", strtotime("{$date2} -6 months"));
if($date3 != $date)
{
$date2 = date("Y-m-t", strtotime("{$date2} -1 months"));
}
(or in your case "m/t/Y")
One simple way is to actually go one month ahead of the day you want and then make the day value zero. Also, mktime() might be easier
$mymonth = 2; // I want the last day of February
echo date('m/d/Y', mktime(0,0,0,$mymonth+1,0,2011));
This should return 2/28/2011.
strtotime does the best it can with conflicting information. Saying
1/31/2011 +1month
would mean advancing to
2/31/2011
but February only has 28 (sometimes 29) days. 2011 isn't a leap year, so the "31st of February" gets normalized to "March 3rd".
The same applies for '12/31/2004 +6month'. That takes you to June 31st, 2005. But June only has 30 days, so the date is normalized to July 1st instead.
I'm trying to get the unix time for date strings that are formatted like so:
'second sunday of march 2010'
'first sunday of november 2010'
I was under the impression that strtotime could handle such a string, but apparently not, as this returns false. How can I convert to unix time when given a day of week, which one of those in the month (ie. first, second, etc.), a month and a year.
This should be possible with strtotime. You could try generating a timestamp of the first day of march using mktime() and adding that as a 2nd parameter (leaving just "first sunday" in the string part):
$timestamp = mktime (0,0,0,3,1,2010); // 1st of march
$first_sunday = strtotime("first sunday", $timestamp);
Not sure how this will handle the first day (March 1st) actually being a sunday. Make sure you check that out.
Also, and maybe this more reliable, check this out - the poster says he got good results with the following notation (quoting):
<?php
strtotime('+0 week sun nov 2009'); // first sunday in nov 2009
strtotime('+1 week sun nov 2009'); // second sunday
strtotime('-1 week sun nov 2009'); // last sunday in oct 2009
?>
As always with strtotime, whatever you pick, make sure you test well, especially the edge cases (1st day of month is a sunday, last day of last month was a sunday....)
Your code works for me on PHP 5.3.0. What version of PHP are you using?
<?php
date_default_timezone_set("Europe/Oslo");
$time_march = strtotime('second sunday of march 2010');
$time_november = strtotime('first sunday of november 2010');
echo date("Y-m-d", $time_march) . " (timestamp: $time_march)\n";
echo date("Y-m-d", $time_november) . " (timestamp: $time_november)\n";
?>
gives:
2010-03-14 (timestamp: 1268521200)
2010-11-07 (timestamp: 1289084400)