Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics

Dashboard
Notifications
Mark all as read
Q&A

yyyy/mm/dd instead expected dd/mm/yyyy format in PHP-created-HTML output

+1
−0

I have an HTML-PHP-CSS contact form with a date field:

<input type="date" name="date" id="cf_input_date"></input>
  • The default value in this field is the W3C built-in default: dd/mm/yyyy
  • The date-picking pattern in this field is the W3C built-in default dd/mm/yyyy

My problem

Everything was allegedly fine until I printed this HTML as output to my email box (via PHP).
In my email box, the output pattern for picked dates is the exact opposite of the pattern I already use in the form:

yyyy/mm/dd

Things I have tried to solve it

  • dir="ltr" or dir="rtl" attributes-values and direction: ltr or direction: rtl properties-values don't help in flipping the output
  • Various CSS tricks all ended in failure
  • Sadly I can't use JavaScript comfortably in this case because the email output appears in a third party email client which doesn't easily allow JavaScript manipulation of the DOM

My question

How to ensure that my HTML-PHP output is in dd/mm/yyyy pattern (as default for the input field itself) instead the "flipped" yyyy/mm/dd?

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

10 comments

According to MDN, the displayed format of input type=date can change according to the browser's locale. My guess is that mail clients might do something similar when interpreting HTML. Not sure if this can be changed (without some JS running on client side). Anyway, when submitted, the value is always sent in ISO 8601 format (yyyy-mm-dd, regardless of the displayed format), which can be properly handled at the backend hkotsubo‭ 7 days ago

Hello @hkotsubo, I don't understand why this ISO standard developers wanted to started from the end :) Also, sadly I almost don't have control about the backend of the email client and use it pretty much "out of box". JohnDoea‭ 7 days ago

Starting from the end means dates that are treated as strings sort correctly. Ever find yourself with a directory full of files named (or prefixed/suffixed) with things like "mar-5-2019" or "12-apr-2020"? Not the most convenient results when you sort by name. Whether this is the reason for the yyyy-mm-dd standard I don't know, but it's a reason I like it. Monica Cellio‭ 7 days ago

Besides the nice lexicographic order you get by default with YYYY-mm-dd, the timestamps are often extended with hour, minute, seconds, milliseconds. By starting with the year , adding them is done in a consistent way (see this). Alexei‭ 7 days ago

ISO 8601 is intended to be "locale agnostic" and unambiguous, and this "biggest to smallest units" approach is IMO a good choice, with advantages already pointed by previous comments. It's an information exchange format, not a human friendly one (although it is friendly once you get used to it) hkotsubo‭ 7 days ago

Show 5 more comments

1 answer

+3
−0

The date is sent in the form submission in ISO 8601 format (year-month-day). You can reformat it in the form handler as follows:

$input_date = $_REQUEST['cf_input_date'];
if ($input_date)
{
    $input_date = DateTime::createFromFormat("Y-m-d", $input_date)->format("d/m/Y");
}
Why does this post require moderator attention?
You might want to add some details to your flag.

2 comments

Sadly the email client automatically changes everything to yyyy-mm-dd format... If I could control the (PHP?) of my email client --- Roundcube, I guess that then it would surly help... JohnDoea‭ 2 days ago

@JohnDoea, I find that hard to believe and suspect that you're misdiagnosing the problem, but I don't think anyone can take this further unless you post at least some of the form handler (probably complete with logging statements and log output to show the formats before and after key steps). Peter Taylor‭ 1 day ago

Sign up to answer this question »