I'm trying to get the first of the current month in PHP, using the date
function, however, the output isn't quite what I'm after, even if I manually change the date string.
//get Current date
$dat=date("Y-m-d H:i:s");
//date with same month and year but first day of month in the above date
$d=date("Y-M-01",strtotime($dat));
//date with first day of month printed correctly
echo $d;
//day not printing correctly. First day is not printing.
echo date('N',$d);
This is output I'm getting:
//This is output of date after setting first day of month
2013-Dec-01
//but when above printed date is used in as date('N',above Printed date)
//the result is 4 and not 1
4
If you want day you need to write.
echo date('d', $d);
And check this to get more info: http://www.php.net/manual/en/function.date.php
First of all, date
function expects second argument to be a timestamp, you're giving it a string ($d
).
Second of all, N
in the date format is a numeric representation of the day of the week, not month. What you're looking for is d
.