How to get the name of the day in PHP using date?

379

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..
535

Answer

Solution:

You can simplify this a lot without converting back and forth betweendate andstrtotime:

$year = date('Y');
$month = date('n');
$lastDay = date('t');

foreach (range(1, $lastDay) as $day) {
    echo date('D, j m Y', mktime(0, 0, 0, $month, $day, $year)), '<br>';
}

See http://php.net/mktime.

Omitting implicit default values and condensing it a bit, you can in fact boil it down to:

foreach (range(1, date('t')) as $day) {
    echo date('D, j m Y', mktime(0, 0, 0, date('n'), $day)), '<br>';
}
Mon, 1 04 2019
Tue, 2 04 2019
Wed, 3 04 2019
...
Tue, 30 04 2019

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 thedate('n') anddate('t') functions happen to be called "in different months". To avoid that possibility entirely, make this operation atomic:

list($year, $month, $lastDay) = explode(' ', date('Y n t'));

foreach (range(1, $lastDay) as $day) {
    echo date('D, j m Y', mktime(0, 0, 0, $month, $day, $year)), '<br>';
}
864

Answer

Solution:

The code below will print out what you need..

$date1 = date('Y-m-01');
$Maximum_Date = date('t', strtotime($date1));

for($Date = 1; $Date <= $Maximum_Date; $Date++)
{
    $thatDay = date('Y-m-'.$Date);
    $day = date('l, j', strtotime($thatDay));
    $Month = date('m', strtotime($thatDay));
    $Year = date('Y', strtotime($thatDay));

    echo $day . ' ' . $Month . ' ' . $Year . '<BR>';
}

Output:

Monday, 1 04 2019
Tuesday, 2 04 2019
Wednesday, 3 04 2019
Thursday, 4 04 2019
Friday, 5 04 2019
...
...

if you change this line

$day = date('l, j', strtotime($thatDay));

TO

$day = date('l, jS', strtotime($thatDay));

The output will be: Monday, 1st 04 2019

119

Answer

Solution:

Use:

$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("D-m-Y",strtotime($Year."-".$Month."-".$Date));
  echo $DataDate. '<br>';;
}
585

Answer

Solution:

You should try like below.

$date1 = date('Y-m-01');
$Month = date('m', strtotime($date1));
$Year = date('Y', strtotime($date1));
$Maximum_Date = date('t', strtotime($date1));

$StartDate=$date1;
$EndDate=($Year."-".$Month."-".$Maximum_Date);
$EndDate=date('Y-m-d',strtotime($EndDate));

while(strtotime($EndDate)>=strtotime($StartDate))
{
    echo date('l, d-m-Y',strtotime($StartDate))."<br/>";
    $StartDate=date('Y-m-d', strtotime($StartDate . ' +1 day'));
}

After execute you will get result like below.

Monday, 01-04-2019
Tuesday, 02-04-2019
Wednesday, 03-04-2019
...................
...................
...................
...................
Saturday, 27-04-2019
Sunday, 28-04-2019
Monday, 29-04-2019
Tuesday, 30-04-2019
518

Answer

Solution:

Add the following line to your loop:

    echo (new DateTime($Date. '-'. $Month .'-'. $Year))->format('l');

It creates a new DateTime object with your data & echo's the day in the format that you requested.

People are also looking for solutions to the problem: php - Submit button is missing

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.