fork download
  1. <?php
  2. $calendar = array();
  3. function my_calendar($month,$year) {
  4.  
  5. // Cria uma matriz contendo abreviações de dias da semana.
  6. $daysOfWeek = array('Domingo','Segunda-feira','Terça-feira','Quarta-feira','Quinta-feira','Sexta-feira','Sábado');
  7.  
  8. // Qual é o primeiro dia do mês em questão?
  9. $firstDayOfMonth = mktime(0,0,0,$month, 1, $year);
  10.  
  11. // Quantos dias este mês contém?
  12. $numberDays = date('t', $firstDayOfMonth);
  13.  
  14. // Recupere algumas informações sobre o primeiro dia do mês em questão.
  15. $dateComponents = getdate($firstDayOfMonth);
  16.  
  17. // Qual é o valor do índice (0-6) do primeiro dia do mês em questão.
  18. $dayOfWeek = $dateComponents['wday'];
  19.  
  20. //cabeçalho
  21.  
  22. $calendar[] = $year;
  23. // foreach($daysOfWeek as $k => $week) {
  24. // array_push($calendar, $week);
  25. //}
  26.  
  27. // Cria os cabeçalhos do calendário
  28.  
  29.  
  30. /* Cria o resto do calendário
  31.   Inicia o contador de dias, começando com o 1º. */
  32.  
  33. $currentDay = 1;
  34.  
  35. $calendar = array();
  36.  
  37. /* A variável $dayOfWeek é usada para
  38.   garantir que o calendário
  39.   display consiste em exatamente 7 colunas. */
  40.  
  41. if ($dayOfWeek > 0) {
  42. $calendar[] = array('week' => $dayOfWeek);
  43. }
  44.  
  45. $month = str_pad($month, 2, "0", STR_PAD_LEFT);
  46.  
  47. while ($currentDay <= $numberDays) {
  48.  
  49. //Sétima coluna (sábado) alcançada. Comece uma nova linha.
  50. if ($dayOfWeek == 7) {
  51. $dayOfWeek = 0;
  52.  
  53. }
  54.  
  55. $currentDayRel = str_pad($currentDay, 2, "0", STR_PAD_LEFT);
  56.  
  57. $date = array('ano' => $year, 'mes' => $month , 'dia' => $currentDayRel);
  58.  
  59. $calendar[]= array('semana' => $daysOfWeek[$dayOfWeek],'data_eng' => "$year-$month-$currentDayRel", 'data'=> $date, 'dia_corrente_num' => $currentDay);
  60.  
  61. //Incremento
  62. $currentDay++;
  63. $dayOfWeek++;
  64.  
  65. }
  66.  
  67.  
  68. // Preencha a linha da última semana do mês, se necessário
  69.  
  70. if ($dayOfWeek != 7) {
  71.  
  72. $remainingDays = 7 - $dayOfWeek;
  73. $calendar[] = array('remainingDays' => $remainingDays);
  74.  
  75. }
  76.  
  77. return $calendar;
  78.  
  79. }
  80.  
  81. $dateComponents = getdate();
  82.  
  83. $month = $dateComponents['mon'];
  84. $year = $dateComponents['year'];
  85.  
  86.  
  87. $result1 = my_calendar($month, $year);
  88. $result2= my_calendar(2, 2019);
  89.  
  90. $results = array(
  91. 'current_month' => $result1, //meses correntes
  92. 'set_month' => $result2 // um mes e ano setado...
  93. );
  94. print_r($results);
  95.  
Success #stdin #stdout 0.02s 24696KB
stdin
Standard input is empty
stdout
Array
(
    [current_month] => Array
        (
            [0] => Array
                (
                    [semana] => Domingo
                    [data_eng] => 2019-09-01
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 01
                        )

                    [dia_corrente_num] => 1
                )

            [1] => Array
                (
                    [semana] => Segunda-feira
                    [data_eng] => 2019-09-02
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 02
                        )

                    [dia_corrente_num] => 2
                )

            [2] => Array
                (
                    [semana] => Terça-feira
                    [data_eng] => 2019-09-03
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 03
                        )

                    [dia_corrente_num] => 3
                )

            [3] => Array
                (
                    [semana] => Quarta-feira
                    [data_eng] => 2019-09-04
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 04
                        )

                    [dia_corrente_num] => 4
                )

            [4] => Array
                (
                    [semana] => Quinta-feira
                    [data_eng] => 2019-09-05
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 05
                        )

                    [dia_corrente_num] => 5
                )

            [5] => Array
                (
                    [semana] => Sexta-feira
                    [data_eng] => 2019-09-06
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 06
                        )

                    [dia_corrente_num] => 6
                )

            [6] => Array
                (
                    [semana] => Sábado
                    [data_eng] => 2019-09-07
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 07
                        )

                    [dia_corrente_num] => 7
                )

            [7] => Array
                (
                    [semana] => Domingo
                    [data_eng] => 2019-09-08
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 08
                        )

                    [dia_corrente_num] => 8
                )

            [8] => Array
                (
                    [semana] => Segunda-feira
                    [data_eng] => 2019-09-09
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 09
                        )

                    [dia_corrente_num] => 9
                )

            [9] => Array
                (
                    [semana] => Terça-feira
                    [data_eng] => 2019-09-10
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 10
                        )

                    [dia_corrente_num] => 10
                )

            [10] => Array
                (
                    [semana] => Quarta-feira
                    [data_eng] => 2019-09-11
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 11
                        )

                    [dia_corrente_num] => 11
                )

            [11] => Array
                (
                    [semana] => Quinta-feira
                    [data_eng] => 2019-09-12
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 12
                        )

                    [dia_corrente_num] => 12
                )

            [12] => Array
                (
                    [semana] => Sexta-feira
                    [data_eng] => 2019-09-13
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 13
                        )

                    [dia_corrente_num] => 13
                )

            [13] => Array
                (
                    [semana] => Sábado
                    [data_eng] => 2019-09-14
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 14
                        )

                    [dia_corrente_num] => 14
                )

            [14] => Array
                (
                    [semana] => Domingo
                    [data_eng] => 2019-09-15
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 15
                        )

                    [dia_corrente_num] => 15
                )

            [15] => Array
                (
                    [semana] => Segunda-feira
                    [data_eng] => 2019-09-16
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 16
                        )

                    [dia_corrente_num] => 16
                )

            [16] => Array
                (
                    [semana] => Terça-feira
                    [data_eng] => 2019-09-17
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 17
                        )

                    [dia_corrente_num] => 17
                )

            [17] => Array
                (
                    [semana] => Quarta-feira
                    [data_eng] => 2019-09-18
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 18
                        )

                    [dia_corrente_num] => 18
                )

            [18] => Array
                (
                    [semana] => Quinta-feira
                    [data_eng] => 2019-09-19
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 19
                        )

                    [dia_corrente_num] => 19
                )

            [19] => Array
                (
                    [semana] => Sexta-feira
                    [data_eng] => 2019-09-20
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 20
                        )

                    [dia_corrente_num] => 20
                )

            [20] => Array
                (
                    [semana] => Sábado
                    [data_eng] => 2019-09-21
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 21
                        )

                    [dia_corrente_num] => 21
                )

            [21] => Array
                (
                    [semana] => Domingo
                    [data_eng] => 2019-09-22
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 22
                        )

                    [dia_corrente_num] => 22
                )

            [22] => Array
                (
                    [semana] => Segunda-feira
                    [data_eng] => 2019-09-23
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 23
                        )

                    [dia_corrente_num] => 23
                )

            [23] => Array
                (
                    [semana] => Terça-feira
                    [data_eng] => 2019-09-24
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 24
                        )

                    [dia_corrente_num] => 24
                )

            [24] => Array
                (
                    [semana] => Quarta-feira
                    [data_eng] => 2019-09-25
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 25
                        )

                    [dia_corrente_num] => 25
                )

            [25] => Array
                (
                    [semana] => Quinta-feira
                    [data_eng] => 2019-09-26
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 26
                        )

                    [dia_corrente_num] => 26
                )

            [26] => Array
                (
                    [semana] => Sexta-feira
                    [data_eng] => 2019-09-27
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 27
                        )

                    [dia_corrente_num] => 27
                )

            [27] => Array
                (
                    [semana] => Sábado
                    [data_eng] => 2019-09-28
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 28
                        )

                    [dia_corrente_num] => 28
                )

            [28] => Array
                (
                    [semana] => Domingo
                    [data_eng] => 2019-09-29
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 29
                        )

                    [dia_corrente_num] => 29
                )

            [29] => Array
                (
                    [semana] => Segunda-feira
                    [data_eng] => 2019-09-30
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 09
                            [dia] => 30
                        )

                    [dia_corrente_num] => 30
                )

            [30] => Array
                (
                    [remainingDays] => 5
                )

        )

    [set_month] => Array
        (
            [0] => Array
                (
                    [week] => 5
                )

            [1] => Array
                (
                    [semana] => Sexta-feira
                    [data_eng] => 2019-02-01
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 01
                        )

                    [dia_corrente_num] => 1
                )

            [2] => Array
                (
                    [semana] => Sábado
                    [data_eng] => 2019-02-02
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 02
                        )

                    [dia_corrente_num] => 2
                )

            [3] => Array
                (
                    [semana] => Domingo
                    [data_eng] => 2019-02-03
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 03
                        )

                    [dia_corrente_num] => 3
                )

            [4] => Array
                (
                    [semana] => Segunda-feira
                    [data_eng] => 2019-02-04
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 04
                        )

                    [dia_corrente_num] => 4
                )

            [5] => Array
                (
                    [semana] => Terça-feira
                    [data_eng] => 2019-02-05
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 05
                        )

                    [dia_corrente_num] => 5
                )

            [6] => Array
                (
                    [semana] => Quarta-feira
                    [data_eng] => 2019-02-06
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 06
                        )

                    [dia_corrente_num] => 6
                )

            [7] => Array
                (
                    [semana] => Quinta-feira
                    [data_eng] => 2019-02-07
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 07
                        )

                    [dia_corrente_num] => 7
                )

            [8] => Array
                (
                    [semana] => Sexta-feira
                    [data_eng] => 2019-02-08
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 08
                        )

                    [dia_corrente_num] => 8
                )

            [9] => Array
                (
                    [semana] => Sábado
                    [data_eng] => 2019-02-09
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 09
                        )

                    [dia_corrente_num] => 9
                )

            [10] => Array
                (
                    [semana] => Domingo
                    [data_eng] => 2019-02-10
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 10
                        )

                    [dia_corrente_num] => 10
                )

            [11] => Array
                (
                    [semana] => Segunda-feira
                    [data_eng] => 2019-02-11
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 11
                        )

                    [dia_corrente_num] => 11
                )

            [12] => Array
                (
                    [semana] => Terça-feira
                    [data_eng] => 2019-02-12
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 12
                        )

                    [dia_corrente_num] => 12
                )

            [13] => Array
                (
                    [semana] => Quarta-feira
                    [data_eng] => 2019-02-13
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 13
                        )

                    [dia_corrente_num] => 13
                )

            [14] => Array
                (
                    [semana] => Quinta-feira
                    [data_eng] => 2019-02-14
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 14
                        )

                    [dia_corrente_num] => 14
                )

            [15] => Array
                (
                    [semana] => Sexta-feira
                    [data_eng] => 2019-02-15
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 15
                        )

                    [dia_corrente_num] => 15
                )

            [16] => Array
                (
                    [semana] => Sábado
                    [data_eng] => 2019-02-16
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 16
                        )

                    [dia_corrente_num] => 16
                )

            [17] => Array
                (
                    [semana] => Domingo
                    [data_eng] => 2019-02-17
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 17
                        )

                    [dia_corrente_num] => 17
                )

            [18] => Array
                (
                    [semana] => Segunda-feira
                    [data_eng] => 2019-02-18
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 18
                        )

                    [dia_corrente_num] => 18
                )

            [19] => Array
                (
                    [semana] => Terça-feira
                    [data_eng] => 2019-02-19
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 19
                        )

                    [dia_corrente_num] => 19
                )

            [20] => Array
                (
                    [semana] => Quarta-feira
                    [data_eng] => 2019-02-20
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 20
                        )

                    [dia_corrente_num] => 20
                )

            [21] => Array
                (
                    [semana] => Quinta-feira
                    [data_eng] => 2019-02-21
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 21
                        )

                    [dia_corrente_num] => 21
                )

            [22] => Array
                (
                    [semana] => Sexta-feira
                    [data_eng] => 2019-02-22
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 22
                        )

                    [dia_corrente_num] => 22
                )

            [23] => Array
                (
                    [semana] => Sábado
                    [data_eng] => 2019-02-23
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 23
                        )

                    [dia_corrente_num] => 23
                )

            [24] => Array
                (
                    [semana] => Domingo
                    [data_eng] => 2019-02-24
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 24
                        )

                    [dia_corrente_num] => 24
                )

            [25] => Array
                (
                    [semana] => Segunda-feira
                    [data_eng] => 2019-02-25
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 25
                        )

                    [dia_corrente_num] => 25
                )

            [26] => Array
                (
                    [semana] => Terça-feira
                    [data_eng] => 2019-02-26
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 26
                        )

                    [dia_corrente_num] => 26
                )

            [27] => Array
                (
                    [semana] => Quarta-feira
                    [data_eng] => 2019-02-27
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 27
                        )

                    [dia_corrente_num] => 27
                )

            [28] => Array
                (
                    [semana] => Quinta-feira
                    [data_eng] => 2019-02-28
                    [data] => Array
                        (
                            [ano] => 2019
                            [mes] => 02
                            [dia] => 28
                        )

                    [dia_corrente_num] => 28
                )

            [29] => Array
                (
                    [remainingDays] => 2
                )

        )

)