Get all occurrence of specific day in a month

后端 未结 2 395
轻奢々
轻奢々 2021-01-26 12:35

Suppose i have a month June 2014. Now i want to get dates of all Mondays in June month.

like Monday is coming on following days so answer will be like following

相关标签:
2条回答
  • 2021-01-26 13:06

    Try this -

    <?php
    $startDate = "2014-06-01";
    $endDate = "2014-06-30";
    
    $startDate = strtotime($startDate);
    $endDate = strtotime($endDate);
    
    for($i = strtotime('Monday', $startDate); $i <= $endDate; $i = strtotime('+1 week', $i))
        echo date('l Y-m-d', $i).PHP_EOL;
    


    DEMO:

    http://3v4l.org/n4ULA

    0 讨论(0)
  • 2021-01-26 13:07

    Try to create an array with all your date with day on key (with variable $day and $date):

    $array = array("Monday" => "2014-06-02", "Tuesday" => "2014-06-03", "Wednesday" => "2014-06-04");
    

    You create a loop to reach all the result :

    foreach($array as $key => $value {
        if($key == "Monday")
           echo $value;
    }
    
    0 讨论(0)
提交回复
热议问题