chodo baggins chodo baggins -4 years ago 107
PHP Question

Returning the first day of a given month in weekday format with PHP

So I want, for the purpose of creating a calendar, to identify what the first weekday of any given month is. I have the following code:


IF (!$_GET) {

// the month in question is linked through a GET form variable in the Ymd format

$thisdaynow=date('Y-m-d', $now);

$monthyear=date('F Y', $now);

$thismonth=date('M', $now);

$thisyear=date('Y', $now);

$weekday=date('l', $now);

$firstday = new DateTime($thisdaynow);
$firstday->modify('first day of this month');
$firstweekday=date('l', $work);
$firstdayweek=date('w', $work);

ECHO 'Today is '.$thisdaynow.'<br />';

ECHO 'The first day of the month was '.$work.'<br />';

ECHO 'Today is a '.$weekday.'.<br />';

ECHO 'The first day of this month was a '.$firstweekday.', the '.$firstdayweek.'th day of the week.<br />';

this returns:

Today is 2013-05-06

The first day of the month was 20130501

Today is a Monday.

The first day of this month was a Saturday, the 6th day of the week.

There are 31 days this month.

Any help on what I'm doing wrong would be greatly appreciated!!!

Answer Source
$inputMonth = '2013-05-01';
$month = date("m" , strtotime($inputMonth));
$year = date("Y" , strtotime($inputMonth));
$getdate = getdate(mktime(null, null, null, $month, 1, $year));
echo $getdate["weekday"];

Produces: Wednesday

If problem persists. Problem might be here:

IF (!$_GET) {

Should be

if (!isset($_GET['month'])) {

This way, you are always assigning to current time() which is why the first day of month is always of the current month.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download