php获取当月有多少天

文章描述:

php如何获取当前月有多少天

PHP获取当前月有多少天,首先获取当前日期,根据当前日期获取天数

<?php

$date=date('Y-m-d');

echo $date;

echo "<br>";

echo date("t",strtotime($date));

 

结果:

2023-08-02
31

 

php获取当前月的所有日期

echo "<pre>";
print_r(getMonthDays());
/**
 * 获取当前月的所有日期
 * @return array
 */
function getMonthDays()
{
     $monthDays = [];
     $firstDay = date('Y-m-01', time());
     $i = 0;
     $lastDay = date('Y-m-d', strtotime("$firstDay +1 month -1 day"));
     while (date('Y-m-d', strtotime("$firstDay +$i days")) <= $lastDay) {
         $monthDays[] = date('Y-m-d', strtotime("$firstDay +$i days"));
         $i++;
     }
     return $monthDays;
}

结果:

Array
(
    [0] => 2023-08-01
    [1] => 2023-08-02
    [2] => 2023-08-03
    [3] => 2023-08-04
    [4] => 2023-08-05
    [5] => 2023-08-06
    [6] => 2023-08-07
    [7] => 2023-08-08
    [8] => 2023-08-09
    [9] => 2023-08-10
    [10] => 2023-08-11
    [11] => 2023-08-12
    [12] => 2023-08-13
    [13] => 2023-08-14
    [14] => 2023-08-15
    [15] => 2023-08-16
    [16] => 2023-08-17
    [17] => 2023-08-18
    [18] => 2023-08-19
    [19] => 2023-08-20
    [20] => 2023-08-21
    [21] => 2023-08-22
    [22] => 2023-08-23
    [23] => 2023-08-24
    [24] => 2023-08-25
    [25] => 2023-08-26
    [26] => 2023-08-27
    [27] => 2023-08-28
    [28] => 2023-08-29
    [29] => 2023-08-30
    [30] => 2023-08-31
)

 

PHP获取当前月及前几个月

echo date("m - Y")."<br>";
echo date("m - Y",strtotime("-1 Months"))."<br>";
echo date("m - Y",strtotime("-2 Months"))."<br>";
echo date("m - Y",strtotime("-3 Months"))."<br>";

08 – 2023
07 – 2023
06 – 2023
05 – 2023

 

上个月第一天

echo date("Y-m-d",strtotime("$firstday -1 month -1 day"));

 

上个月和下个月

$date = '2023-08-09';

echo date("Y-m-d",strtotime("$date -1 month -1 day"));
echo date("Y-m-d",strtotime("$date +1 month -1 day"));

2023-07-08

2023-09-08

发布时间:2023/08/02

发表评论