Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get all days and dates for the selected month of a year

Tags:

date

php

how to get both days and dates for selected month of selected year and show in tables. e-g: i have tried so far.

<?php
$num_of_days = cal_days_in_month(CAL_GREGORIAN, 9, 2003);
for( $i=1; $i<= $num_of_days; $i++)
    $dates[]= str_pad($i,2,'0', STR_PAD_LEFT);
/*echo "<pre>";
print_r($dates);
echo "</pre>";*/
?>
<table>
    <tr>
<?php
foreach($dates as $date){
    echo"<td>".$date."</td>";
}
?>
    </tr>
</table>

this executes for me this code.

<table>
    <tbody><tr>
<td>01</td><td>02</td><td>03</td><td>04</td><td>05</td><td>06</td><td>07</td><td>08</td><td>09</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td>30</td>    </tr>
</tbody></table>

but i want another row below the dates row. which should show day related to that date??

by day i mean :monday, tues, wed etc. by date i mean : 1, 2, 3, 4 etc

so it would be like

<tr><td>1</td><td>2</td><td>3</td><td>4</td>
<tr><td>Mon</td><td>Tues</td><td>Wed</td><td>Thursday</td>

I hope i could explain my self..

like image 928
Sizzling Code Avatar asked Dec 12 '14 13:12

Sizzling Code


People also ask

How can I get dates of the month?

function getAllDaysInMonth(year, month) { const date = new Date(year, month, 1); const dates = []; while (date. getMonth() === month) { dates. push(new Date(date)); date.

How to get day of month in vb net?

Dim today = Date. Now. Date If today. Day = 9 AndAlso today.

Who decided how many days in a month?

Julius Caesar modified the Roman calendar in 46 B.C. to make each month have either 30 or 31 days, with the exception of Februarius, which had 29 days and gained an extra day every fourth year. Quintilis was later renamed Julius in his honor.


2 Answers

You can use date('l') to get the corresponding day name:

<?php
$date = '2003-09-01';
$end = '2003-09-' . date('t', strtotime($date)); //get end date of month
?>
<table>
    <tr>
    <?php while(strtotime($date) <= strtotime($end)) {
        $day_num = date('d', strtotime($date));
        $day_name = date('l', strtotime($date));
        $date = date("Y-m-d", strtotime("+1 day", strtotime($date)));
        echo "<td>$day_num <br/> $day_name</td>";
    }
    ?>
    </tr>
</table>
like image 99
Kevin Avatar answered Oct 18 '22 06:10

Kevin


A different approach could be to use the DateTime object:

$aDates = array();
$oStart = new DateTime('2014-12-01');
$oEnd = clone $oStart;
$oEnd->add(new DateInterval("P1M"));

while ($oStart->getTimestamp() < $oEnd->getTimestamp()) {
    $aDates[] = $oStart->format('D d');
    $oStart->add(new DateInterval("P1D"));
}

Then to print:

foreach ($aDates as $day) {
    echo $day;
}

For more information about the format parameters, you can refer to: http://php.net/manual/en/function.date.php

like image 36
Peter Avatar answered Oct 18 '22 08:10

Peter