-1

Is there a quick way to format a \DateTime into HTML-friendly Local date and time string (e.g. 1986-01-28T11:38:00.01)?

Both DATE_W3C and c will produce a timezone, which doesn't work with datetime-local.

I know I can do "Y-m-d\TH:i:s", but was curious if there's a slightly more elegant shorthand by now?

ᴍᴇʜᴏᴠ
  • 4,804
  • 4
  • 44
  • 57
  • What do you mean with more elegant? isn't it elegant enough that you use datetime->format for that purpose? – Mihai Matei Oct 28 '20 at 12:04
  • I'm not talking about `format()` itself, I'm talking about the arguments to it. I was looking for a shorthand for `Y-m-d\TH:i:s`, that way it's faster to type and avoids hardcoding the format. – ᴍᴇʜᴏᴠ Oct 28 '20 at 12:26
  • In that case, you could use one of the constants defined in `\DateTimeInterface`. You are probably looking for something similar to: `(new \DateTime)->format(\DateTimeInterface::ISO8601)`. There is even the `W3C` constant there – Mihai Matei Oct 28 '20 at 12:30

1 Answers1

1

You can use one of the constants defined on the DateTimeInterface:

const ATOM = 'Y-m-d\TH:i:sP';
const COOKIE = 'l, d-M-Y H:i:s T';
const ISO8601 = 'Y-m-d\TH:i:sO';
const RFC822 = 'D, d M y H:i:s O';
const RFC850 = 'l, d-M-y H:i:s T';
const RFC1036 = 'D, d M y H:i:s O';
const RFC1123 = 'D, d M Y H:i:s O';
const RFC2822 = 'D, d M Y H:i:s O';
const RFC3339 = 'Y-m-d\TH:i:sP';
const RFC3339_EXTENDED = 'Y-m-d\TH:i:s.vP';
const RFC7231 = 'D, d M Y H:i:s \G\M\T';
const RSS = 'D, d M Y H:i:s O';
const W3C = 'Y-m-d\TH:i:sP';

Example:

(new \DateTime)->format(\DateTimeInterface::W3C)
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50