Clinton J
Clinton J

Reputation: 2123

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:

    $today=date('Y-m-d');

    IF (!$_GET) {
    $now=time();
    }
    ELSE {
    $now=strtotime($_GET['month']);
    }

    // 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');
    $work=$firstday->format('Ymd');
    $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!!!

Upvotes: 3

Views: 4785

Answers (2)

edwardmp
edwardmp

Reputation: 6601

$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.

http://phpfiddle.org/main/code/4ja-928

Upvotes: 4

Tigger
Tigger

Reputation: 9130

This code:

$m = 1;
$d = 1;
$y = 2013;

do {
    $time = strtotime($y.'-'.$m.'-'.$d);
    $month = date('F',$time);
    $dayOfMonth = date('l',$time);
    $totalDays = date('t',$time);
    echo 'First day of '.$month.', '.$y.' is '.$dayOfMonth.' 
          ('.$totalDays.' days in '.$month.').<br />';
} while (++$m < 13);

Will display these results:

First day of January, 2013 is Tuesday (31 days in January).
First day of February, 2013 is Friday (28 days in February).
First day of March, 2013 is Friday (31 days in March).
First day of April, 2013 is Monday (30 days in April).
First day of May, 2013 is Wednesday (31 days in May).
First day of June, 2013 is Saturday (30 days in June).
First day of July, 2013 is Monday (31 days in July).
First day of August, 2013 is Thursday (31 days in August).
First day of September, 2013 is Sunday (30 days in September).
First day of October, 2013 is Tuesday (31 days in October).
First day of November, 2013 is Friday (30 days in November).
First day of December, 2013 is Sunday (31 days in December).

Lots more info in the PHP manual at: http://php.net/date

Upvotes: 2

Related Questions