Getting first weekday in a month with strtotime

php first day of month
php get month from date
fjy date format
php date
php get month name
get first and last date of week in php
php first day of next month
php get first day of week from week number

I'm trying to figure out the first wednesday of a given month using strtotime, but the "first wednesday" argument fails whenever the first wednesday happens to fall on the 1st.

For a more general illustration of this problem, see the following code and result:

$mon = strtotime("December 2010 first monday");
$tue = strtotime("December 2010 first tuesday");
$wed = strtotime("December 2010 first wednesday");
$thu = strtotime("December 2010 first thursday");
$fri = strtotime("December 2010 first friday");
$sat = strtotime("December 2010 first saturday");
$sun = strtotime("December 2010 first sunday");

echo strftime("%m/%d/%y", $mon) . "<br>";
echo strftime("%m/%d/%y", $tue) . "<br>";
echo strftime("%m/%d/%y", $wed) . "<br>";
echo strftime("%m/%d/%y", $thu) . "<br>";
echo strftime("%m/%d/%y", $fri) . "<br>";
echo strftime("%m/%d/%y", $sat) . "<br>";
echo strftime("%m/%d/%y", $sun) . "<br>";

Results in:

12/06/10
12/07/10
12/08/10
12/02/10
12/03/10
12/04/10
12/05/10

Notice something? Shouldn't one day of the week coincide with the 1st of the month? But it never does, and instead the second instance of the day, on the 8th, is always returned.

Anyone have an explanation for this?

I don't have any explanation as I'm also dazzled, but I managed to find out how you get the correct date by omitting the "first", like so:

$ php -r 'echo date("m/d/y", strtotime("December 2010 Wednesday"));'
12/01/10

$ php -r 'echo date("m/d/y", strtotime("December 2010 Thursday"));'
12/02/10

$ php -r 'echo date("m/d/y", strtotime("December 2010 Friday"));'
12/03/10

$ php -r 'echo date("m/d/y", strtotime("December 2010 Saturday"));'
12/04/10

$ php -r 'echo date("m/d/y", strtotime("December 2010 Sunday"));'
12/05/10

$ php -r 'echo date("m/d/y", strtotime("December 2010 Monday"));'
12/06/10

$ php -r 'echo date("m/d/y", strtotime("December 2010 Tuesday"));'
12/07/10

PHP: Get the first Monday of a month (and more), This is a small cheat sheet for PHP's strtotime function, which can be used to convert textual sentences such as "next Friday" into UNIX timestamps. I’m trying to figure out the first wednesday of a given month using strtotime, but the “first wednesday” argument fails whenever the first wednesday happens to fall on the 1st. For a more general illustration of this problem, see the following code and result:

Building off the answers provided, one needs to be aware of differences in relative date formatting between php 5.2 and php 5.3.

TEST:

$date1 = strtotime("first wednesday of 2010-12");
$date2 = strtotime("first wednesday 2010-12");
$date3 = strtotime("wednesday 2010-12");

5.2 Results:

1969-12-31
2010-12-08
2010-12-01

5.3 Results:

2010-12-01
2010-12-08
2010-12-01

Therefore only the third method returns correct results across PHP 5 versions.

Relative Formats - Manual, This page describes the different relative date/time formats that the strtotime(), Get the first day of a month, in a typical calendar month, typically the Monday in� In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions. 5.1.0: Now returns FALSE on failure, instead of -1. 5.1.0

It's just your formatting that is incorrect. You need to include of:

echo strftime("%m/%d/%y", strtotime("first Monday of December 2010"));
echo strftime("%m/%d/%y", strtotime("first Tuesday of December 2010"));
echo strftime("%m/%d/%y", strtotime("first Wednesday of December 2010"));
echo strftime("%m/%d/%y", strtotime("first Thursday of December 2010"));
echo strftime("%m/%d/%y", strtotime("first Friday of December 2010"));
echo strftime("%m/%d/%y", strtotime("first Saturday of December 2010"));
echo strftime("%m/%d/%y", strtotime("first Sunday of December 2010"));

Prints:

12/06/10
12/07/10
12/01/10
12/02/10
12/03/10
12/04/10
12/05/10

The accepted solution works, but you would encounter problems if you wanted to find the second weekday:

echo strftime("%m/%d/%y", strtotime("December 2010 second Wednesday"));

Prints:

12/15/10

There are some examples given in the docs:

http://www.php.net/manual/en/datetime.formats.relative.php

The second block of notes in the above documentation explains how of affects the date calculation process:

Also observe that the "of" in "ordinal space dayname space 'of' " and "'last' space dayname space 'of' " does something special.

  1. It sets the day-of-month to 1.
  2. "ordinal dayname 'of' " does not advance to another day. (Example: "first tuesday of july 2008" means "2008-07-01").
  3. "ordinal dayname " does advance to another day. (Example: "first tuesday july 2008" means "2008-07-08", see also point 4 in the list above).
  4. "'last' dayname 'of' " takes the last dayname of the current month. (Example: "last wed of july 2008" means "2008-07-30")
  5. "'last' dayname" takes the last dayname from the current day. (Example: "last wed july 2008" means "2008-06-25"; "july 2008" first sets the current date to "2008-07-01" and then "last wed" moves to the previous Wednesday which is "2008-06-25").

How to get first day of week in PHP?, Use strtotime() function to get the first day of week using PHP. So, in PHP to get the Sunday of as first day of a week, consider the Sunday of previous week. d, m and Y will display the day, month and year respectively. PHP 5.2.7: In earlier versions, if requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month it would incorrectly add one week to the returned timestamp. This has been corrected now PHP 5.1.0: Returns FALSE on failure (earlier versions returns -1), and issues E_STRICT and E_NOTICE time zone errors

I found this note in the PHP manual.

"In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions."

Calculating past and future dates < PHP, The strtotime function doesn't just convert date strings to timestamp Months and years are also valid input but not always useful in 3 days ago: Sunday 2nd August (2020-08-02); -3 days: Sunday 2nd In the list above we can see that ' Saturday', 'this Saturday' and 'first Saturday' are all equivalent. PHP: Get the first Monday of a month (and more) This is a small cheat sheet for PHP’s strtotime function, which can be used to convert textual sentences such as “next Friday” and “last Monday” into UNIX timestampsand formatted dates.

PHP 5.5.11

$firstWeekdayOfMonth = new DateTime('first weekday 0 jun 2016');

I don't know why, but the zero kind of force the strtotime parser to start at the beginning of the month considering the first day.

Returning the first day of a given month in weekday format with PHP , _GET) { $now=time(); } ELSE { $now=strtotime($_GET['month']); } // the month in question is linked through a GET form variable in the Ymd format� Use strtotime () function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date () function to convert timestamp date into understandable date. strtotime () Function: The strtotime () function returns the result in timestamp by parsing the time string.

Datetime first monday of month - Build, Hello, I have first day of the month saved in a datetime variable. This for example echo date(“j, d-M-Y”, strtotime(“next monday”)); Does not work in Uipath though so decided to How to get Last, First Monday of this Month. In the previous post I covered how you can return the last weekday of the month. This post will focus on the formula that returns the first weekday of the month. Assuming cell A1 contains a date the formula is: =EOMONTH(A1,-1)+1+CHOOSE(WEEKDAY(EOMONTH(A1,-1)+1,2),0,0,0,0,0,2,1) This formula will work with Excel 2007 and later versions.

In #!/bin/bash. Unix command to get date of the first of a weekday in , In ksh93 : $ printf '%(%F)T\n' 'next month, first Monday' 2017-04-03. bash , since 4.2 now supports a %(<format)T in its printf buitin, but not the� How to set the first day of the week in sap.m.DatePicker's calendar? How to get day of month, day of year and day of week in android using offset date time API class? Java Program to get day of week for the last day of each month in 2019; How to get the first day of next month in MySQL? How to get first day of every corresponding month in MySQL

Get date from monday and friday for the week by selecting month , All Mondays in a Given Month: $time = strtotime("first Monday of ".$monthname." " .$year); while($time <= strtotime("� //Get the first date of the previous month. echo date ( "Y-m-d" , strtotime ( "first day of previous month" ) ) ; If you were to run the example above on the 31st of October, it should return the 1st of September.

Comments
  • Well you could look at php source code if you <i>are</i> really curious, as for me, let's move on :D
  • It doesn't seem to work entirely. I'm utilizing this solution on June 12th and I get 12/15/2014. Perhaps it's relative to the week of the month it currently is?
  • the 'of' format only works on PHP >=5.3, and fails on 5.2. See my answer for test results.
  • @Yarin: Well spotted. Date handling in >=5.3 appears to be much more reliable than earlier versions. It's a shame there are so many potential pitfalls.