略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: DateInterval::createFromDateString

2024-12-22

DateInterval::createFromDateString

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

DateInterval::createFromDateStringSets up a DateInterval from the relative parts of the string

说明

public static DateInterval::createFromDateString(string $datetime): DateInterval|false

Uses the normal date parsers and sets up a DateInterval from the relative parts of the parsed string.

参数

datetime

A date with relative parts. Specifically, the relative formats supported by the parser used for DateTimeImmutable, DateTime, and strtotime() will be used to construct the DateInterval.

返回值

Returns a new DateInterval instance on success, 或者在失败时返回 false.

更新日志

版本 说明
8.2.0 Only the from_string and date_string properties will be visible when a DateInterval is created with this method.

范例

示例 #1 Parsing valid date intervals

<?php
// Each set of intervals is equal.
$i = new DateInterval('P1D');
$i DateInterval::createFromDateString('1 day');

$i = new DateInterval('P2W');
$i DateInterval::createFromDateString('2 weeks');

$i = new DateInterval('P3M');
$i DateInterval::createFromDateString('3 months');

$i = new DateInterval('P4Y');
$i DateInterval::createFromDateString('4 years');

$i = new DateInterval('P1Y1D');
$i DateInterval::createFromDateString('1 year + 1 day');

$i = new DateInterval('P1DT12H');
$i DateInterval::createFromDateString('1 day + 12 hours');

$i = new DateInterval('PT3600S');
$i DateInterval::createFromDateString('3600 seconds');
?>

示例 #2 Parsing special relative date intervals

<?php
$i 
DateInterval::createFromDateString('last day of next month');
var_dump($i);

$i DateInterval::createFromDateString('last weekday');
var_dump($i);

Output of the above example in PHP 8.2:

object(DateInterval)#1 (2) {
  ["from_string"]=>
  bool(true)
  ["date_string"]=>
  string(22) "last day of next month"
}
object(DateInterval)#2 (2) {
  ["from_string"]=>
  bool(true)
  ["date_string"]=>
  string(12) "last weekday"
}

以上例程在 PHP 8 中的输出类似于:

object(DateInterval)#1 (16) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(1)
  ["d"]=>
  int(0)
  ["h"]=>
  int(0)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["f"]=>
  float(0)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(2)
  ["invert"]=>
  int(0)
  ["days"]=>
  bool(false)
  ["special_type"]=>
  int(0)
  ["special_amount"]=>
  int(0)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(0)
}
object(DateInterval)#2 (16) {
  ["y"]=>
  int(0)
  ["m"]=>
  int(0)
  ["d"]=>
  int(0)
  ["h"]=>
  int(0)
  ["i"]=>
  int(0)
  ["s"]=>
  int(0)
  ["f"]=>
  float(0)
  ["weekday"]=>
  int(0)
  ["weekday_behavior"]=>
  int(0)
  ["first_last_day_of"]=>
  int(0)
  ["invert"]=>
  int(0)
  ["days"]=>
  bool(false)
  ["special_type"]=>
  int(1)
  ["special_amount"]=>
  int(-1)
  ["have_weekday_relative"]=>
  int(0)
  ["have_special_relative"]=>
  int(1)
}
add a noteadd a note

User Contributed Notes 3 notes

up
2
Anonymous
12 years ago
DateInterval::createFromDateString ( string $time )

When the manual says "Uses the normal date parsers" it means that this function cannot take $time = ISO8601 strings like "P7D".  If you want to use those, you must use the constructor.
up
0
BobRay
26 days ago
Weeks are always converted to days and added to any days specified. As far as I can tell, this is the only calculation done by the method. Somewhat surprisingly, this works (though obviously  it would be a poor coding practice):

$di = DateInterval::createFromDateString('62 weeks + 1 day + 2 weeks + 2 hours + 70 minutes');
  /* Stored as 0 years, 449 days, 2 hours, 70 minutes */
up
0
BobRay
26 days ago
Unlike the DateInterval constructor, this method will accept negative values, so this works:

      $di = DateInterval::createFromDateString('1 year - 10 days');
           /* Stored as 1 year, -10 days */

官方地址:https://www.php.net/manual/en/dateinterval.createfromdatestring.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3