php get all dates between start date and end date
Due to project requirements, it is necessary to obtain data within a specified date range and process it on a daily basis.
For example, it is necessary to process the data of each day in the date range from 2016-06-01 to 2016-06-05. First, you need to obtain the date of each day in this date range, and then perform processing in a loop.
<code><span><span><?php</span><span>$date</span> = <span>array</span>(<span>'2016-06-01'</span>,<span>'2016-06-02'</span>,<span>'2016-06-03'</span>,<span>'2016-06-04'</span>,<span>'2016-06-05'</span>); <span>foreach</span>(<span>$date</span><span>as</span><span>$d</span>){ <span>// 执行处理</span> } <span>?></span></span></code>
If the date range spans a large period (2015-09-01 to 2016-06-30), and there are situations where it spans years and months (leap months), manually creating the date array is too time-consuming and unreasonable.
So I wrote the following method to get the date of each day in the specified date range. The code is as follows:
<code><span><span><span><?php</span> /** * 获取指定日期段内每一天的日期 *<span> @param</span> Date $startdate 开始日期 *<span> @param</span> Date $enddate 结束日期 *<span> @return</span> Array */</span><span><span>function</span><span>getDateFromRange</span><span>(<span>$startdate</span>, <span>$enddate</span>)</span>{</span><span>$stimestamp</span> = strtotime(<span>$startdate</span>); <span>$etimestamp</span> = strtotime(<span>$enddate</span>); <span>// 计算日期段内有多少天</span><span>$days</span> = (<span>$etimestamp</span>-<span>$stimestamp</span>)/<span>86400</span>+<span>1</span>; <span>// 保存每天日期</span><span>$date</span> = <span>array</span>(); <span>for</span>(<span>$i</span>=<span>0</span>; <span>$i</span><<span>$days</span>; <span>$i</span>++){ <span>$date</span>[] = date(<span>'Y-m-d'</span>, <span>$stimestamp</span>+(<span>86400</span>*<span>$i</span>)); } <span>return</span><span>$date</span>; } <span>// demo</span><span>$date</span> = getDateFromRange(<span>'2016-02-25'</span>,<span>'2016-03-05'</span>); print_r(<span>$date</span>); <span>?></span></span></span></code>
Output:
<code><span>Array</span> ( [<span>0</span>] => <span>2016</span>-<span>02</span>-<span>25</span> [<span>1</span>] => <span>2016</span>-<span>02</span>-<span>26</span> [<span>2</span>] => <span>2016</span>-<span>02</span>-<span>27</span> [<span>3</span>] => <span>2016</span>-<span>02</span>-<span>28</span> [<span>4</span>] => <span>2016</span>-<span>02</span>-<span>29</span> [<span>5</span>] => <span>2016</span>-<span>03</span>-<span>01</span> [<span>6</span>] => <span>2016</span>-<span>03</span>-<span>02</span> [<span>7</span>] => <span>2016</span>-<span>03</span>-<span>03</span> [<span>8</span>] => <span>2016</span>-<span>03</span>-<span>04</span> [<span>9</span>] => <span>2016</span>-<span>03</span>-<span>05</span> )</code>
The above introduces php to get all the dates between the start date and the end date, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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



Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

When developing using PHP programs, you often encounter some warning or error messages. Among them, one error message that may appear is: PHPWarning:date()expectsparameter2tobelong,stringgiven. The error message means: the second parameter of the function date() is expected to be a long integer (long), but what is actually passed to it is a string (string). So, we

If you're looking for a way to automatically create and name files and folders based on system timestamps, you've come to the right place. There is a super simple way to accomplish this task. The created folders or files can then be used for various purposes such as storing file backups, sorting files based on date, etc. In this article, we will explain in some very simple steps how to automatically create files and folders in Windows 11/10 and name them according to the system’s timestamp. The method used is a batch script, which is very simple. Hope you enjoyed reading this article. Section 1: How to automatically create and name a folder based on the current timestamp of the system Step 1: First, navigate to the parent folder where you want to create the folder,

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

1. Introduction The Date class in the java.util package represents a specific time, accurate to milliseconds. If we want to use our Date class, then we must introduce our Date class. Writing the year directly into the Date class will not produce the correct result. Because Date in Java is calculated from 1900, so as long as you fill in the first parameter with the number of years since 1900, you will get the year you want. The month needs to be subtracted by 1, and the day can be inserted directly. This method is rarely used, and the second method is commonly used. This method is to convert a string that conforms to a specific format, such as yyyy-MM-dd, into Date type data. First, define an object of Date type Date

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

How to get millisecond representation of date using getTime() method of Date class In Java, Date class is a class used to represent date and time. It provides many useful methods to manipulate and obtain information about date objects. Among them, the getTime() method is an important method in the Date class, which can return the millisecond representation of the date object. Next, we will detail how to use this method to obtain the millisecond representation of a date, and provide corresponding code examples. Using the Date class

There are many excellent calendar libraries and date libraries in Python for us to use. These libraries can help us handle date and calendar related operations. Next, I will introduce you to several common choices and provide corresponding code examples. Datetime library: Datetime is Python's built-in date and time processing module. It provides many date and time related classes and methods, which can be used to process dates, times, time differences and other operations. Sample code: importdatetime#Get the current date
