使用DateTime的PHP每月第一天

使用DateTime的PHP每月第一天,php,Php,我有一个php的日期时间: $datetime=new DateTime(); 我如何获得一周中的某一天: $datetime->format("w"); 但是对于当前$datetime月的第一天?只需使用->modify()方法进行相应调整: $datetime = new DateTime(); $datetime->modify('first day of this month'); $output = $datetime->format("w"); echo $ou

我有一个php的日期时间:

$datetime=new DateTime();
我如何获得一周中的某一天:

$datetime->format("w");

但是对于当前
$datetime
月的第一天?

只需使用
->modify()
方法进行相应调整:

$datetime = new DateTime();
$datetime->modify('first day of this month');
$output = $datetime->format("w");
echo $output; // 0 - sunday
没有相对日期时间字符串:

$dt = new DateTime();
$dt->setDate($dt->format('Y'), $dt->format('m'), 1);
$output = $dt->format('w');
echo $output;
您可以尝试以下方法:

需要PHP5.3才能工作(PHP5.3中引入了“工作的第一天”)

否则,上述示例是唯一的方法:

l=一周中某一天的完整文本表示

$datetime = new DateTime();
$datetime->modify('first day of this month');
echo $output = $datetime->format("l"); //Sunday
备选方案:

echo date("l", strtotime(date('Y-m-01'))); //Sunday

或者将相对日期/时间格式字符串传递给DateTime构造函数,而不是事后修改。@PaulCrovella是的,我忘记了它们与
strottime
共享相同的解析,只需使用这行代码
echo date(“l”,strottime(“本月的第一天”)
///sunday
可能重复的