How to get the name of the day in PHP using date?
Is it possible to get the name of the day in the row of numbers converted todate
?
I have Code :
$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));
for($Date = 1; $Date <= $Maximum_Date; $Date++){
$DataDate = $Date . ' ' . $Month . ' ' . $Year . '<BR>';
echo $DataDate;
}
Result :
1 04 2019
2 04 2019
3 04 2019
etc..
What I want is to change it to display the name of the day on that date
For Example Date in April :
Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
etc..
[UPDATE] April 15, 2019
Refer to comments, I see documentation here and apply withmktime();
So I Update The Code :
$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));
for($Date = 1; $Date <= $Maximum_Date; $Date++){
echo date("l, d m Y", mktime(0, 0, 0, $Month, $Date, $Year)) . '<br>';
}
And get the result :
Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
etc..
Answer
Solution:
You can simplify this a lot without converting back and forth between
date
andstrtotime
:See http://php.net/mktime.
Omitting implicit default values and condensing it a bit, you can in fact boil it down to:
Note that this code has a minuscule potential to break, should you execute it right at the second in which one month rolls over to the next, and the
date('n')
anddate('t')
functions happen to be called "in different months". To avoid that possibility entirely, make this operation atomic:Answer
Solution:
The code below will print out what you need..
Output:
if you change this line
TO
The output will be: Monday, 1st 04 2019
Answer
Solution:
Use:
Answer
Solution:
You should try like below.
After execute you will get result like below.
Answer
Solution:
Add the following line to your loop:
It creates a new DateTime object with your data & echo's the day in the format that you requested.