How can I get the days of the month using php?

enter image description hereI want to show the dates of the corresponding days in a specific month. How should I do it? I manage to enter the month and show all the dates.

<html>
<body>
<form method="POST">
Day (Eg: Sunday) : <input name="day" required type="text" size="30" style="height:30px;" /> <br/><br/>
Month (Eg: 7) : <input name="month" required type="text" size="30" style="height:30px;" /> <br/><br/>
<input name="Submit" type="submit" value="Log In" style="background-color: #2E9AFE; border: 1px solid #084B8A;  padding: 1px 3px; color:#fff; width:60px; height:30px;" />
</form>

<?php

	$day=$_POST['day'];
	$month=$_POST['month'];
	
    function getDates($y, $m)
	{
		return new DatePeriod(
			new DateTime("first sunday of $y-$m"),
			DateInterval::createFromDateString('next sunday'),
			new DateTime("last day of $y-$m")
		);
	}

	foreach (getDates(2015, $month) as $getDay) {
		echo $getDay->format("l, Y-m-d\n");
	}

	
?>
</body>
</html>
Run code
+4
source share
2 answers

If you want to specify only the month as input, repeat it with the total number of days in the month

Here is eval

<?php
$Days=array();
$Month = 8;
for($d=1; $d<=31; $d++)
{
    $Time=mktime(12, 0, 0, $Month, $d, '2015');          
    if (date('m', $Time)==$Month)       
        $Days[]=date('Y-m-d-D', $Time);
}
echo '<pre>';
print_r($Days);
echo '</pre>';
?>

Note:

  • I gave the year directly, where you can give it a dynamic (user choice).

  • The days of the month are indicated directly as 31, you will find the number of days and give it too.

Update:

Because the OP wants to get only dates Monday.

<?php
$Days=array();
$Month = 12;
for($d=1; $d<=31; $d++)
{
    $Time=mktime(12, 0, 0, $Month, $d, '2014');          
    if (date('m', $Time)==$Month)       
        $Day=date('D', $Time);
        if($Day=='Mon')
        {
        $Days[]=date('Y-m-d-D', $Time);
        }
}
echo '<pre>';
print_r($Days);
echo '</pre>';
?>

Here's an updated score

+1
source
 $first_date = date('Y-m-d', strtotime("first  Sunday of this month", strtotime('01-07-2015')));
 $last_date  = date('Y-m-d', strtotime("last  Sunday of this month", strtotime('01-07-2015')));
 $days[]       = $first_date;
while(strtotime($first_date) <= strtotime($last_date)) {

    $first_date =   date('Y-m-d', strtotime("next  Sunday ", strtotime($first_date)));
    if(strtotime($first_date) <= strtotime($last_date)) {
        $days[] = $first_date;
    }

}
  echo '<pre>';print_r($days);exit;
0
source

All Articles