百科狗-知识改变命运!
--

date_create() - php 日期时间函数

梵高1年前 (2023-11-21)阅读数 17#技术干货
文章标签别名

date_create()

date_create() - php 日期时间函数

(PHP 5 >= 5.2.0, PHP 7)

别名DateTime::__construct()

说明

此函数是该函数的别名:DateTime::__construct()

If you want to create the DateTime object directly from a timestamp use this

See also: http://bugs.php.net/bug.php?id=40171
"String in a format accepted by strtotime()" is not 100% truth - you cannot pass timezone info in the string used as DateTime constructor, while you can do it with strtotime(). It may be a problem if you would like to create a date from GMT time and then display it in your local timezone, for example:

[red. your claim that "is not 100% truth" is incorrect, you're seeing desired behavior here. The timezone passed as 2nd argument is used as a default fall back, in case the parsed string doesn't provide TZ information.]
So if you want to convert date between different timezones, you have to create two DateTimeZone objects - one for the input and one for output, like this:

I'm not sure if this is a bug or desired behaviour.
[red. you don't have to do create two DateTimeZone objects, this works too:

]
If you are getting an error like this:
Exception: DateTime::__construct(): Failed to parse time string (13/02/2013) at position 0 (1): Unexpected character in DateTime->__construct()
Note that when you create a new date object using a format with slashes and dashes (eg 02-02-2012 or 02/02/2012) it must be in the mm/dd/yy(yy) or mm-dd-yy(yy) format (rather than british format dd/mm/yy)! Months always before years (the american style) otherwise you'll get an incorrect date and may get an error like the one above (where PHP is crashing on trying to decode a 13th month).
Can catch you off guard because everything seems to be working fine and dandy until you hit a value over 12.
@Marton Bodonyi 
This is not exactly true.
The following code:
[code]
echo "1\n";
print_r(date_create('13-02-2013'));
echo "2\n";
print_r(date_create('13/02/2013'));
echo "3\n";
print_r(new DateTime('13-02-2013'));
echo "4\n";
print_r(new DateTime('13/02/2013'));
[/code]
produces the following output:
[code]
1
DateTime Object
(
  [date] => 2013-02-13 00:00:00.000000
  [timezone_type] => 3
  [timezone] => Europe/London
)
2
3
DateTime Object
(
  [date] => 2013-02-13 00:00:00.000000
  [timezone_type] => 3
  [timezone] => Europe/London
)
4

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (13/02/2013) at position 0 (1): Unexpected character' in /test.php:23 Stack trace: #0 /test.php(23): DateTime->__construct('13/02/2013') #1 {main} thrown in /test.php on line 23
[/code] Which means that only slashes format actually wants you to use American order of date parts. Hence you should use 'dd-mm-yyyy' or 'mm/dd/yyyy' Which is weird, and should not be such. PHP version is 5.4.40
DateTime will recognise any number up to 12 as a [month], and any number up to 31 as a [day]; it calculates the resulting date to be [day] days after the start of [month]. This means that when a datetime object is created with more days than are found in that month, the date will be beyond the end of the month. 
See also
date_create_from_format
http://php.net/manual/en/function.date-create-from-format.php
Indeed as mentioned above the constructor for DateTime will allow the day of month upto 31 to be accepted for all months of the year. You should use checkdate if you wish to check that the day of the month represents a valid gregorian calendar date.
Notice php by default assume the give string as such format:
'-'  is  'y-m-d'
'/'  is  'm/d/y'
Unless the given string has Y or M,
that is year is written as full year '2019', or month is written as English shorthand 'Jan', 
the default assumption will be applied, where the date might be incorrect.
The following code show a quick test: (true as of php 7.2)
$date = [
  '2019-1-3',
  '19-1-3',
  '3-1-2019',
  '3-Jan-19',
  '3-1-19', //php assume as y-m-d not d-m-y
  '2019-3-1',
  '19-3-1',
  '1-3-2019',
  '1-3-19',
  '2019/3/1',
  '19/3/1', //fail, php think is month 19
  '1/3/2019', //php think is m/d/y
  '1/3/19'
];
//Y-M-d
foreach($date as $i => $d){
  echo $i ."\r\n";
  var_dump(date_format(date_create($d), 'Y-M-d'));
  echo "\r\n";
}
You should also be aware that DateTime has very lax date validation rules. It appears that all months have a maximum of 31 days.
For example, these will work fine:

We all know that these dates are not real and are invalid so I think a good idea is to do some further validation on dates before creating a DateTime object with them.
The construct have some problem with date validate.
 
When you set 
$datetime = DateTime::createFromFormat('Y-m-d H:i:s','2009-02-30 00:00:00');
it will be accepted but convert to 2009-03-02 00:00:00, it means the date was count on and plus 2 days

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)