Comments (8)
I fixed this in the documentation last week: e5373bc
from doc-en.
Furthermore, I don't believe the | modifier is working at all.
IIRC, I had previously used
$date=DateTime::createFromFormat('Y-m-d|', '2021-08-12 13:14:15');
for example in order to get 2021-08-12 00:00:00, but now that seems to return false, at least in PHP 7.4 and 8.0.
I thought it was meant to reset all fields that haven't been parsed at that point.
Seem I now need to put 'Y-m-d|+' to have it ignore the H:i:s component.
DateTime::getLastErrors seems to be empty when this fails.
from doc-en.
The documentation seems flipped to me.
format character | Description |
---|---|
! |
Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to zero-like values ( 0 for hour, minute, second and fraction, 1 for month and day, 1970 for year and UTC for timezone information) |
| |
Resets all fields (year, month, day, hour, minute, second, fraction and timezone information) to zero-like values if they have not been parsed yet |
Yet, it behaves the other way around: with the position of !
being important and the position of |
not making a difference at all. |
anywhere behaves as if !
was placed at the beginning.
<?php
var_dump(DateTime::createFromFormat('!Y-m-d', '2023-08-23'));
# 2023-08-23 00:00:00.000000
var_dump(DateTime::createFromFormat('Y-!m-d', '2023-08-23'));
# 1970-08-23 00:00:00.000000
var_dump(DateTime::createFromFormat('Y-m-d!', '2023-08-23'));
# 1970-01-01 00:00:00.000000
var_dump(DateTime::createFromFormat('Y-m-d|', '2023-08-23'));
# 2023-08-23 00:00:00.000000
var_dump(DateTime::createFromFormat('|Y-m-d', '2023-08-23'));
# 2023-08-23 00:00:00.000000
Tested at https://onlinephp.io/ with php 8.2.9.
from doc-en.
@WoodrowShigeru What about it is "flipped"?
!
in the string will reset the current "state" of the date to be the Unix epoch. It also means anything parsed so far is discarded, so putting this anywhere except the beginning of the string would be weird.
|
resets to the epoch too, but only the parts of the date that haven't been parsed so far. Because of how it works, the effect is the same regardless of where it's placed in the format string.
They're very similar, but the main difference between the two is the "feel" of how they work: !
is suited to the beginning of the format string and conveys the intention of "start from zero instead of the current time, then...", while |
is suited for the end of the format string and conveys "...and make everything else be zero".
!Y-m-d = reset everything to zero/the epoch, then parse a date
Y-m-d| = parse a date, then reset the other parts not listed here to zero
from doc-en.
The "if they have not been parsed yet" part should be written behind !
imo. What's written left of the character is parsed and will not be reset anymore. The |
variant always resets all fields regardless of parsing.
from doc-en.
The "if they have not been parsed yet" part should be written behind
!
imo. What's written left of the character is parsed and will not be reset anymore.
Uh, no? !
will reset everything, including fields that have been parsed. This is extremely easy to demonstrate:
// if ! resets everything then this will be 1970-01-01
// if ! resets unparsed fields then this will be 2023-08-27
DateTime::createFromFormat("Y-m-d!", "2023-08-27")
The
|
variant always resets all fields regardless of parsing.
Also no. |
only resets fields that have not been parsed. Here's a similar demonstration:
// if | resets everything then this will be 1970-01-01
// if | resets unparsed fields then this will be 2023-08-27
DateTime::createFromFormat("Y-m-d|", "2023-08-27")
I'm not sure how but you definitely have those two backwards.
from doc-en.
!
will reset everything
Admittedly, in post #671 (comment) I might have mixed up left and right – which does not help me convey my point.
The point is that the position of !
matters:
DateTime::createFromFormat('Y-!m-d', '2023-08-23');
# → 1970-08-23 00:00:00.000000, the "08" and "23" remain.
This only resets some fields. Some != all.
The
|
variant always resets all fields regardless of parsing.
That is unfortunate phrasing on my part because I keep seeing "parsing" as "everything on one side of that special character". A better phrasing of what I meant would have been: the |
variant always resets the same fields regardless of the position.
After reading all parts in this thread more carefully and doing more tests, I guess I can now understand the "if they have not been parsed yet" part standing in the |
description.
However, I would agree with the rest of the documentation only if !
at the beginning would be the only possible way to use the !
. It's not. You can write it in the middle of the format string, and then it behaves differently.
It's true (but misleadingly phrased) that !
resets all fields only if you know that it first resets the fields and then parses the date – as you've described. But the documentation doesn't mention that part at all. That's, like, outside knowledge … or insider knowledge …
from doc-en.
This only resets some fields. Some != all.
No, it does reset all of them...
I think I see what you're missing: the !
happens at the moment it's encountered, not like at the end when the process is (nearly) complete and everything has been read in. Parsing the format Y-!m-d
means specific actions in a specific order, read from left to right:
- Parse the
Y
and skip a hyphen (Y=2023, m=current month, d=current day) - Reset all the fields (Y=1970, m=1, d=1)
- Parse the
m
and skip a hyphen (Y=1970, m=8, d=1) - Parse the
d
(Y=1970, m=8, d=23)
The year was parsed before getting reset so that value was lost, while the month and day were parsed after getting reset so their values were kept.
So yes, while you can put |
anywhere, it doesn't really make sense to have it anywhere except the beginning: why bother parsing something like a year if you're just going to reset it to 1970 anyway?
(I can imagine some unusual exceptions where it could make sense, but they really are unusual exceptions. Most people would want "reset everything because I don't want PHP using the current date/time for default values", and you do that with !
by putting it at the beginning.)
from doc-en.
Related Issues (20)
- RFC: Disjunctive Normal Form Types - invalid intersection example HOT 2
- OpenSSL error cURL and MSSQL HOT 1
- PHP_OUTPUT_HANDLER_PROCESSED is only available as of PHP 8.4 HOT 4
- ReflectionProperty::getDefaultValue() and promoted properties HOT 1
- ob_start PHP_OUTPUT_HANDLER_FLUSHABLE wrong ob_end_flush instead of ob_flush HOT 2
- Support more than parameter attributes attributes HOT 7
- Make it clearer that Exceptions cannot be cloned HOT 6
- set_error_handler and set_exception_handler do not necessarily return a callable
- Empty `id` attribute in the `Examples` section HOT 5
- Dark mode or theme HOT 1
- Wrong return type for settype
- wordwrap() should mention that it only supports ASCII characters HOT 2
- in_array HOT 3
- curl_setopt page, the CURLOPT_XFERINFOFUNCTION constant
- docs fail to load
- PHP 8.2.18 HOT 2
- DateTimeImmutable::createFromFormat doesn't mention "p"
- Why is documentation so 'lacking' for PHP source code? HOT 2
- Error in Changelog for the function "hash" (php.net) HOT 1
- `PREG_UNMATCHED_AS_NULL` description
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from doc-en.