PHP: 遍历出两个时间之间的日期 Date function to display all dates between two dates

There is the DatePeriod class.

EXAMPLE:

$begin = new DateTime('2013-02-01');
$end = new DateTime('2013-02-13');

$daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);

foreach($daterange as $date){
    echo $date->format("Y-m-d") . "<br>";
}

(P1D stands for period of one day, see DateInterval for further documentation)

$period = new DatePeriod(
     new DateTime('2010-10-01'),
     new DateInterval('P1D'),
     new DateTime('2010-10-05')
);

封装函数:

function date_range_to_array($start='', $end='', $include_last_day = TRUE, $repeat='day', $format='m/d/Y')
{
	$days = array();
	
	if($start AND $end)
	{
		$end = DateTime::createFromFormat($format, $end);
		if($include_last_day) $end = $end->modify('+1 day');
		
		$daterange = new DatePeriod( 
					DateTime::createFromFormat($format, $start), 
					new DateInterval(($repeat=='week'?'P1W':($repeat=='month'?'P1M':'P1D'))), 
					$end);
		foreach($daterange as $day) $days[] = $day->format($format);
	}
	
	return $days;
}

PHP新特性DateTime常用方法整理

对DateTime对象使用的方法进行了一些整理,方便以后的查找和翻阅
实例化对象前面加 \ 表示的是,在命名空间中使用原生的类,如果没有使用命名空间的话,可以把前面的 \ 给删除掉

1. 输出当前时间

$datetime = new \DateTime;
print_r($datetime->format('Y-m-d H:i:s'));

2. 输出给定的时间

$datetime = new \DateTime('2016-06-13');
print_r($datetime);

3. 根据给定的时间格式化为自己想要的时间

$datetime = \DateTime::createFromFormat('Ymd', '20160618');
print_r($datetime->format('Y-m-d'));

4. 输出Unix时间戳格式(方法1如果是1990年以前的会返回负数,而方法2则会返回false)

//方法1(php5.2):
$datetime = new \DateTime();
echo $datetime->format('U');exit;

//方法2(php5.3)推荐
$datetime = new \DateTime();
echo $datetime->getTimestamp();exit;

5. 根据给定的时间戳格式化为给定的时间

$datetime = new \DateTime();
$datetime->setTimestamp(1465783744);
echo $datetime->format('Y-m-d H:i:s');

6. 两个日期时间比对,年与年比对,月与月比对……

$datetime1 = new \DateTime('2016-01-01 10:11:18');
$datetime2 = new \DateTime('2017-05-11 22:21:21');
$interval = $datetime1->diff($datetime2);
print_r($interval->format('%Y'));//%表示使用格式化,R表示是大于这个日期(+),还是小于这个日期(-),a表示大于或小于多少天,时分秒正常使用y,m,d,h,i,s

7. 创建长度为几天前的时间

DateInterval构造函数的参数是一个表示时间间隔约定的字符串,这个时间间隔约定以字母P开头,后面跟着一个整数,最后是一个周期标识符,限定前面的整数。有效周期标识符如下: Y(年) M(月) D(日) W(周) H(时) M(分) S(秒) 间隔约定中既可以有时间也可以有日期,如果有时间需要在日期和时间之间加上字母T,例如,间隔约定P2D表示间隔两天,间隔约定P2DT5H2M表示间隔两天五小时两分钟。

$datetime = new \DateTime();
$interval = new \DateInterval('P2DT5H');
//或者使用createFromDateString方法
//$interval = \DateInterval::createFromDateString('1 month');
//修改DateTime实例
$datetime->add($interval);
echo $datetime->format('Y-m-d H:i:s');

8. 创建几天前的时间

$datetime = new \DateTime();
$interval = new \DateInterval('P2DT5H');
$datetime->sub($interval);
echo $datetime->format('Y-m-d H:i:s');
//ps:有个modify方法,这个方法是减去30,并不是像前推1天,输出的还是12月
$datetime = new \DateTime('2014/12/31');
$datetime->modify( '-1 month' );
print_r($datetime);exit;

9. 重置当前的DateTime对象的时间不同的日期,传递年,月,日

$datetime = new \DateTime();
$datetime->setDate(2015, 2, 28);
echo $datetime->format('Y-m-d');exit;

10. 重置当前的DateTime对象的时间不同的时间,传递时,分,秒(可选参数)

$datetime = new \DateTime();
$datetime->setTime(20, 20, 24);
echo $datetime->format('Y-m-d H:i:s');exit;

11. 格式化时间前更改时间的时区

$timezone = new \DateTimeZone('Asia/Calcutta');
$datetime = new \DateTime();
$datetime->setTimezone($timezone);
print_r($datetime->format('Y-m-d H:i:s'));exit;

12. 返回时区

$date = new \DateTime(null, new DateTimeZone('Asia/Shanghai'));
$tz = $date->getTimezone();
echo $tz->getName();

13. 计算两个时区的偏移值

$dateTimeZoneTaipei = new \DateTimeZone("Asia/Taipei");
$dateTimeZoneJapan = new \DateTimeZone("Asia/Tokyo");
$dateTimeTaipei = new \DateTime("now", $dateTimeZoneTaipei);
$dateTimeJapan = new \DateTime("now", $dateTimeZoneJapan);
$timeOffset = $dateTimeZoneJapan->getOffset($dateTimeTaipei);
print_r($timeOffset);exit;

14. 返回时间间隔,多长时间

$interval = new \DateInterval('P2Y4DT6H8M');
echo $interval->format('%d days');

15. 迭代输出距离当前日期的前几天日期。

DatePeriod类的构造方法接受三个参数而且都必须提供 一个DateTime实例,表示迭代开始的日期和时间 一个DateInterval实例,表示下一个日期和时间的间隔 一个整数,表示迭代的总次数 第四个参数是可选的,用于显式指定周期的结束日期和时间,如果迭代时想要排除开始日期和时间,可以把构造方法的最后一个参数设为DatePeriod::EXCLUDE_START_DATE常量:

$datetime = new \DateTime();
$interval = \DateInterval::createFromDateString('-1 day');
$period = new \DatePeriod($datetime, $interval, 3);
foreach ($period as $date) {
    echo $date->format('Y-m-d'), PHP_EOL;
}

有效的周期标志如下:

  • Y(年)
  • M(月)
  • D
  • W
  • H
  • M(分)
  • S
    间隔的周期中M即表示月,又表示分。所以怎么区分呢?前3个表示日期,后面的表示时间,这就需要用字母T来分隔。可以使用T2M表示间隔两秒。

    $dateStart = new \DateTime();
    $dateInterval = DateInterval::createFromDateString('-1 day');
    $datePeriod = new DatePeriod($dateStart, $dateInterval, 3);
    foreach ($datePeriod as $date) {
    echo $date->format('Y-m-d'), PHP_EOL;
    }

DateTimeZone类:

<?php
$timezone = new DateTimeZone('Asia/Shanghai');
$datetime = new DateTime('2017-07-14', $timezone);
//使用setTimeZone()方法修改DateTime实例的时区
$dateTIme->setTimezone(new DateTimeZone('Asia/Hongkong'));

最后一个例子来演示 DatePeriod 类。它用来对循环的事件进行迭代。向它传入开始时间、结束时间和间隔区间,会得到这其中所有的事件。

<?php
// output all thursdays between $start and $end
$periodInterval = DateInterval::createFromDateString('first thursday');
$periodIterator = new DatePeriod($start, $periodInterval, $end, DatePeriod::EXCLUDE_START_DATE);
foreach ($periodIterator as $date) {
    // output each date in the period
    echo $date->format('Y-m-d') . ' ';
}
The following characters are recognized in the format parameter string
formatcharacter Description Example parsable values
Day
d and j Day of the month, 2 digits with or without leading zeros 01 to 31 or 1 to 31
D and l A textual representation of a day Mon through Sun or Sunday through Saturday
S English ordinal suffix for the day of the month, 2 characters. It’s ignored while processing. st, nd, rd or th.
z The day of the year (starting from 0) 0 through 365
Month
F and M A textual representation of a month, such as January or Sept January through December or Jan through Dec
m and n Numeric representation of a month, with or without leading zeros 01 through 12 or 1 through 12
Year
Y A full numeric representation of a year, 4 digits Examples: 1999 or 2003
y A two digit representation of a year Examples: 99 or 03
Time
a and A Ante meridiem and Post meridiem am or pm
g and h 12-hour format of an hour with or without leading zero 1 through 12 or 01 through 12
G and H 24-hour format of an hour with or without leading zeros 0 through 23 or 00 through 23
i Minutes with leading zeros 00 to 59
s Seconds, with leading zeros 00 through 59
u Microseconds (up to six digits) Example: 45, 654321
Timezone
e, O, P and T Timezone identifier, or difference to UTC in hours, or difference to UTC with colon between hours and minutes, or timezone abbreviation Examples: UTC, GMT, Atlantic/Azores or+0200 or +02:00 or EST, MDT
Full Date/Time
U Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) Example: 1292177455
Whitespace and Separators
(space) One space or one tab Example:
# One of the following separation symbol: ;, :, /, .,,, , ( or ) Example: /
;, :, /, ., ,, , (or ) The specified character. Example:
? A random byte Example: ^ (Be aware that for UTF-8 characracters you might need more than one ?. In this case, using * is probably what you want instead)
* Random bytes until the next separator or digit Example: * in Y-*-d with the string 2009-aWord-08 will match aWord
! Resets all fields (year, month, day, hour, minute, second, fraction and timzone information) to the Unix Epoch Without !, all fields will be set to the current date and time.
| Resets all fields (year, month, day, hour, minute, second, fraction and timzone information) to the Unix Epoch if they have not been parsed yet Y-m-d| will set the year, month and day to the information found in the string to parse, and sets the hour, minute and second to 0.
+ If this format specifier is present, trailing data in the string will not cause an error, but a warning instead Use DateTime::getLastErrors() to find out whether trailing data was present.

 

更多参考:https://www.kancloud.cn/thinkphp/php-the-right-way/3165

 

本文:PHP: 遍历出两个时间之间的日期 Date function to display all dates between two dates

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.