DateTime::__construct() - php 日期时间类
DateTime::__construct()
date_create
(PHP 5 >= 5.2.0, PHP 7)
返回一个新的 DateTime 对象
说明
面向对象风格
publicDateTime::__construct([string $time="now"[,DateTimeZone$timezone=NULL
]])过程化风格date_create([string $time="now"[,DateTimeZone$timezone=NULL]]): DateTime
返回一个新的 DateTime 对象。
参数
$time日期/时间字符串。正确格式的说明详见日期与时间格式。
如果这个参数为字符串"now"表示获取当前时间。如果同时指定了$timezone参数,那么获取指定时区的当前时间。
$timezoneDateTimeZone对象,表示要获取哪个时区的$time。
如果省略了$timezone参数,那么会使用当前时区。
Note:当$time参数是 UNIX 时间戳(例如@946684800),或者已经包含时区信息(例如2010-01-28T15:00:00+02:00)的时候,$timezone参数和当前时区都将被忽略。
返回值
返回一个新的 DateTime 对象实例,或者在发生错误的时候返回过程化风格在失败时返回FALSE
。。
错误/异常
如果发生错误,会抛出Exception。
更新日志
版本 | 说明 |
---|---|
7.1 | 微秒部分不再是'00000'了,而是真实的微秒数据。 |
5.3.0 | 如果$time参数不是一个有效的日期/时间格式,会抛出异常。在之前的版本中是会发出一个错误。 |
范例
Example #1DateTime::__construct()例程
面向对象风格
过程化风格
以上例程会输出:
2000-01-01
Example #2DateTime::__construct()的复杂用法
以上例程的输出类似于:
2000-01-01 00:00:00-05:00 2000-01-01 00:00:00+12:00 2010-04-24 10:24:16-04:00 2010-04-25 02:24:16+12:00 2000-01-01 00:00:00+00:00 2000-03-01 00:00:00-05:00
参见
- DateTime::createFromFormat() 根据给定的格式解析日期时间字符串
- DateTimeZone::__construct() 创建新的DateTimeZone对象
- 日期时间格式
- date.timezoneini 设置
date_default_timezone_set()
设定用于一个脚本中所有日期时间函数的默认时区- DateTime::getLastErrors() 获取警告和错误信息
checkdate()
验证一个格里高里日期
There's a reason for ignoring the time zone when you pass a timestamp to __construct. That is, UNIX timestamps are by definition based on UTC. @1234567890 represents the same date/time regardless of time zone. So there's no need for a time zone at all.
The theoretical limits of the date range seem to be "-9999-01-01" through "9999-12-31" (PHP 5.2.9 on Windows Vista 64): Dates above 10000 and below -10000 do not throw errors but produce weird results:
Note that the DateTime ctor also accepts boolean false and empty strings, and treats them the same as NULL (i.e. result is current date and time). This may lead to unexpected results if you forward function return values without explicitly checking them first. Empty arrays and boolean true trigger PHP warnings OTOH. (checked with PHP 5.5.18)
It is worth noting: If you have not setup a default timezone, an Exception (or error if PHPIf time cannot be parsed an exception of type Exception is thrown which can be caught, however an E_WARNING is emitted as well. This might be confusing if you are converting warnings to exceptions in your error or shutdown handler.A definite "gotcha" (while documented) that exists in the __construct is that it ignores your timezone if the $time is a timestamp. While this may not make sense, the object does provide you with methods to work around it.Also forgot to mention, that MySQL "zeroed" dates do not throw an error but produce a non-sensical date: Another good reason to write your own class that extends from DateTime.About constructing a DateTime, instead of just using the year it seems working when date matches the pattern "YYYY-MM" - Returns the year correctly: "YYYY-MM" $ cat date.php $ php date.php 2015 - Ignores the given input and returns the year from NOW: "YYYY" $ cat date.php $ php date.php 2019 RESULTS/SCOPE: - Linux (May 24, 2019) - PHP 7.2.17 (cli)Does NOT work. You get UTC always. See comment http://php.net/manual/en/datetime.construct.php#106979 use relative format instead: see http://php.net/manual/en/datetime.formats.relative.phpSince PHP 7.1 behavior of this constructor without arguments have changed. From now on microseconds are filled with actual value. In versions results in 2016-07-13Using this in a try catch to verify a string is in a valid date format is unreliable. Single letter strings used for the first argument (time string) of the constructor allows a new instance of the class to be created, without any exception or error. So the below code would not fail or throw an exception. So $time would not be an empty string. $value = 'y'; try { $time = new \DateTimeZone($value); } catch (\Exception $e) { $time = ''; }Be careful working with MySQL dates representing point of transition to Daylight Saving Time. The constructor of DateTime will convert timezone abbreviation to DST but not the time.Although this function throws an exception on invalid $time values (empty strings, for example), the exception can't be caught because it's a fatal exception. Use functions such as checkdate() and strtotime() to validate the string first. should be changed to remove the try/catch block, since it's misleading."The $timezone parameter and the current timezone are ignored when the $time parameter […] is a UNIX timestamp." Watch out – this means that these two are NOT equivalent, they result in different timezones (unless your current timezone is GMT):When passing a non US or SQL date string the date will be formatted incorrectly. // UK date d/m/Y. $date_time = "08/03/2016 00:00:00"; $dt = new DateTime($date_time, new DateTimeZone("Europe/London")); // Gives 2016-08-03 echo $dt->format("Y-m-d");Impossible times due to daylight savings are handled by this function in a way similar to impossible dates, with the difference that this is not an error (i.e. a consequent call to DateTime::getLastError() yields nothing). For example: In the timezone "Europe/Berlin" on Sunday, March 30 2014 there was no 02:30 am, because that our is being skipped due to daylight savings on that day. That is similar to how, for example, Febuary 29, 2014 would be handled, which would be interpreted as March 1, 2014. The difference is, that with the date that would be an error, with the time it is not. Ambigous times due to daylight savings are handled as the second possibility. For example the time 2:30 am occurred twice on October 26, 2014 in the timezone "Europe/Berlin". Note that "2014-10-26T02:30:00+0200", one hour earlier, would be a correct answer as well.This seems to work as expected, at least now:
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)