3

I have a datetime in php:

$datetime=new DateTime();

How can I get day of week:

$datetime->format("w");

But for first day of current $datetime month?

Maciek Semik
  • 1,778
  • 21
  • 37
  • just use this single line code `echo date("l",strtotime('first day of this month'));` `///sunday` – Manjeet Barnala May 05 '16 at 05:34
  • Possible duplicate of [The first day of the current month in php using date\_modify as DateTime object](http://stackoverflow.com/questions/2094797/the-first-day-of-the-current-month-in-php-using-date-modify-as-datetime-object) – user3942918 May 05 '16 at 06:06

2 Answers2

8

Just use the ->modify() method to adjust accordingly:

$datetime = new DateTime();
$datetime->modify('first day of this month');
$output = $datetime->format("w");
echo $output; // 0 - sunday

Without the relative date time string:

$dt = new DateTime();
$dt->setDate($dt->format('Y'), $dt->format('m'), 1);
$output = $dt->format('w');
echo $output;
Kevin
  • 41,329
  • 12
  • 52
  • 68
0

You can try with this:

Requires PHP 5.3 to work ("first day of" is introduced in PHP 5.3).

Otherwise the example above is the only way to do it:

l = A full textual representation of the day of the week

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

Alternative:

echo date("l", strtotime(date('Y-m-01'))); //Sunday
Murad Hasan
  • 9,418
  • 2
  • 19
  • 40