Home > Backend Development > PHP Tutorial > How to get all dates between start and end date in php

How to get all dates between start and end date in php

墨辰丷
Release: 2023-03-28 13:06:02
Original
1584 people have browsed it

This article mainly introduces the method of PHP to obtain all dates between the start and end dates, involving PHP's related calculation skills for date and time. Friends who need it can refer to it

The details are as follows:

/**
 * 获取指定日期段内每一天的日期
 * @param Date $startdate 开始日期
 * @param Date $enddate  结束日期
 * @return Array
 */
function getDateFromRange($startdate, $enddate){
  $stimestamp = strtotime($startdate);
  $etimestamp = strtotime($enddate);
  // 计算日期段内有多少天
  $days = ($etimestamp-$stimestamp)/86400+1;
  // 保存每天日期
  $date = array();
  for($i=0; $i<$days; $i++){
    $date[] = date(&#39;Y-m-d&#39;, $stimestamp+(86400*$i));
  }
  return $date;
}
$startdate = &#39;2016-08-29&#39;;
$enddate = &#39;2016-09-29&#39;;
// demo
$date = getDateFromRange($startdate,$enddate);
print_r($date);
Copy after login

The running results are as follows:

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

The above is the summary of this article All content, I hope it will be helpful to everyone's study.


Related recommendations:

PHP method of sorting a two-dimensional associative array according to one of the fields

phpMethod to dynamically transfer data to highcharts

phpA simple method to implement user login

The above is the detailed content of How to get all dates between start and end date in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template