0

I'm trying to create a calendar in php. I need to get the weekday (sunday, monday, etc) of the first day of the month. My code so far is:

<?php
$current_month = date("n");
$current_year = date("Y");
$first_day_of_month = new DateTime();
$first_day_of_month -> setDate($current_year,$current_month,1);
?>

I'm stuck on how to get the weekday of the set date (the first of the month). Any suggestions?

Vince
  • 2,596
  • 11
  • 43
  • 76

4 Answers4

1

You can use the format function of this class and give the format you desire.

Here is the list of date format that should work:

date format

example:

$first_day_of_month->format('D');

Note: Take a look at the class definition for more question:

DateTime

MatRt
  • 3,494
  • 1
  • 19
  • 14
1

Try,

 echo date("l", mktime(0,0,0,date("n"),1,date("Y")));

In your case,

echo date("l", mktime(0,0,0,$current_month,1,$current_year));
shapeshifter
  • 2,967
  • 2
  • 25
  • 39
0

this is a extended code for you

<?php
$current_month = date("n");
$current_year = date("Y");
$first_day_of_month = new DateTime();
$first_day_of_month -> setDate($current_year,$current_month,1);

$timestamp = strtotime($first_day_of_month);

$day = date('D', $timestamp);
var_dump($day);

?>

this will give you that day .

Tarun Kumar
  • 508
  • 3
  • 14
0

$timestamp = strtotime($current_year-$current_month-1); $day = date('D', $timestamp);

echo $day;

I hope above mentioned code will work for you.

Pawan