0

I'm making a calendar but I ran into a problem. I have been trying to find out how to get the first day of a month. I used to do it like this:

$start = date('d', strtotime("$year-$month-1")); // For this month '2015-9' it returns 1

Now I know I need to add 1 empty element in the calendar to start at the correct day.

But now I would like to do this with the DateTime class but I have no idea of how to do this. I would like to know if something similar to the above code is doable with the DateTime class.

Update

Lets asume That my calendar is below.

|S|M|T|W|T|F|S|
|0|1|2|3|4|5|6|

Each month starts on a different day and I would like to know this days number.

SuperDJ
  • 7,488
  • 11
  • 40
  • 74

1 Answers1

1

I'm pretty sure the first day of any given month is always numbered 1.. But assuming you want other properties of this month as well; you can use:

$date = new DateTime('2010-09-21');
$date->modify('first day of this month');
echo $date->format('r');

After your update; my answer needs only a small tweak; output $date->format('N'); to get the day-of-the-week number. An overview of the formats you can use is in the documentation

Sjon
  • 4,989
  • 6
  • 28
  • 46