


How to get the number of days of the month and date array in php based on the year and month
The example in this article describes how PHP obtains the number of days in the current month and the date array based on the year and month. Share it with everyone for your reference, the details are as follows:
function get_day( $date ) { $tem = explode('-' , $date); //切割日期 得到年份和月份 $year = $tem['0']; $month = $tem['1']; if( in_array($month , array( 1 , 3 , 5 , 7 , 8 , 01 , 03 , 05 , 07 , 08 , 10 , 12))) { // $text = $year.'年的'.$month.'月有31天'; $text = '31'; } elseif( $month == 2 ) { if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) ) //判断是否是闰年 { // $text = $year.'年的'.$month.'月有29天'; $text = '29'; } else{ // $text = $year.'年的'.$month.'月有28天'; $text = '28'; } } else{ // $text = $year.'年的'.$month.'月有30天'; $text = '30'; } return $text; } echo get_day('2016-8-1');
The operation result is: 31
Transformation, return date array:
/** * 获取当月天数 * @param $date * @param $rtype 1天数 2具体日期数组 * @return */ function get_day( $date ,$rtype = '1') { $tem = explode('-' , $date); //切割日期 得到年份和月份 $year = $tem['0']; $month = $tem['1']; if( in_array($month , array( 1 , 3 , 5 , 7 , 8 , 01 , 03 , 05 , 07 , 08 , 10 , 12))) { // $text = $year.'年的'.$month.'月有31天'; $text = '31'; } elseif( $month == 2 ) { if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) ) //判断是否是闰年 { // $text = $year.'年的'.$month.'月有29天'; $text = '29'; } else{ // $text = $year.'年的'.$month.'月有28天'; $text = '28'; } } else{ // $text = $year.'年的'.$month.'月有30天'; $text = '30'; } if ($rtype == '2') { for ($i = 1; $i <= $text ; $i ++ ) { $r[] = $year."-".$month."-".$i; } } else { $r = $text; } return $r; } var_dump(get_day('2016-8-1','2'));
The running results are as follows:
array(31) { [0]=> string(8) "2016-8-1" [1]=> string(8) "2016-8-2" [2]=> string(8) "2016-8-3" [3]=> string(8) "2016-8-4" [4]=> string(8) "2016-8-5" [5]=> string(8) "2016-8-6" [6]=> string(8) "2016-8-7" [7]=> string(8) "2016-8-8" [8]=> string(8) "2016-8-9" [9]=> string(9) "2016-8-10" [10]=> string(9) "2016-8-11" [11]=> string(9) "2016-8-12" [12]=> string(9) "2016-8-13" [13]=> string(9) "2016-8-14" [14]=> string(9) "2016-8-15" [15]=> string(9) "2016-8-16" [16]=> string(9) "2016-8-17" [17]=> string(9) "2016-8-18" [18]=> string(9) "2016-8-19" [19]=> string(9) "2016-8-20" [20]=> string(9) "2016-8-21" [21]=> string(9) "2016-8-22" [22]=> string(9) "2016-8-23" [23]=> string(9) "2016-8-24" [24]=> string(9) "2016-8-25" [25]=> string(9) "2016-8-26" [26]=> string(9) "2016-8-27" [27]=> string(9) "2016-8-28" [28]=> string(9) "2016-8-29" [29]=> string(9) "2016-8-30" [30]=> string(9) "2016-8-31" }
The above is the content of the method of PHP getting the number of days and date array of the current month according to the year and month. More For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Validator can be created by adding the following two lines in the controller.
