I have a unix timestamp (int) in PHP. I want to display this value in a nice manner in the HTML5 datetime input element. I would like to be able to have users see this value in a nice presentable manner, as well as edit it. Is there any nice way of doing this, or am I fooling myself and I am going to have to fuss around with lots of string manipulation?
Asked
Active
Viewed 3.8k times
4 Answers
14
HTML5 Input time
is something like this : 1985-04-12T23:20:50.52
You can do that in PHP like this :
echo date("Y-m-d\TH:i:s");
Output
2012-10-02T19:12:49
HTML with Timestamp
<input type="datetime" value="<?php echo date("Y-m-d\TH:i:s",$timestamp); ?>"/>

Baba
- 94,024
- 28
- 166
- 217
-
That will go in the value attribute of the `` element? Also, I think you mant `.` not `,`, just so it does not confuse anybody else. – thatidiotguy Oct 02 '12 at 17:21
-
3`echo` allows `,` to separate output. – Jason McCreary Oct 02 '12 at 17:23
-
1And i learned something new today. Thank you for being gracious about my supposed correction. – thatidiotguy Oct 02 '12 at 17:35
-
Not only is `,` allowed with `echo`, but it's faster than `.`. – Zenexer Nov 19 '13 at 01:12
-
date() is wrong, when you need a specific timezone. use strftime and set timezone. – user706420 Oct 09 '14 at 22:32
-
This works for me among tens of formation `"Y-m-d\TH:i:s"` – Nam G VU Dec 25 '15 at 05:25
-
very nice solution when dealing with html5's native datetime – Waqas Shakeel Sep 03 '21 at 13:09
10
The other examples are both the old way to produce a valid timeStamp format for the TIME element -- the new hotness in php:
<time><?php echo date(DATE_W3C); ?></time>
That will generate a format like:
"2013-05-19T06:40:08+00:00"

cameron
- 189
- 2
- 5
-
The question asks specifically about `input[type=datetime]` *not* `time[datetime]`. Thank you for introducing me to `date()`'s [predefined constants](http://php.net/manual/en/class.datetime.php#datetime.constants.types), though! I will stick to `date('c')` which also results in an identical [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) string as well hehe. – Alastair Feb 21 '14 at 17:43
4
According to the HTML5 Specification for input type="datetime", it should be as simple as setting the value
attribute.
value="<?php echo date('c', $timestamp); ?>"
See PHP's date() function for additional formatters.

Jason McCreary
- 71,546
- 23
- 135
- 174
3
Datetime doesn't work for me on chrome, datetime-local allows me to choose date and time from popup.
example
<input type="datetime-local" name="xyz" value="<?php echo date("Y-m-d\TH:i:s",time()); ?>"/>
time() functions is not relevant ,I am using it to make it easy for user to choose from current.. time after or before.

Zaheer
- 221
- 1
- 3