简体   繁体   中英

PHP get weekday of setDate()

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?

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

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));

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 .

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

echo $day;

I hope above mentioned code will work for you.

Pawan

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM