简体   繁体   English

PHP得到setDate()的工作日

[英]PHP get weekday of setDate()

I'm trying to create a calendar in php. 我正在尝试在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. 您可以使用此类的format函数并提供所需的格式。

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); $ timestamp = strtotime($ current_year- $ current_month-1); $day = date('D', $timestamp); $ day = date('D',$ timestamp);

echo $day; echo $ day;

I hope above mentioned code will work for you. 我希望上面提到的代码对你有用。

Pawan 爬完

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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