Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
PHP timestamp to HTML5 input type=datetime element
HTML5 datetime input elements require a specific format: YYYY-MM-DDTHH:MM:SS. In PHP, you can convert timestamps to this format using the date() function with the appropriate format string.
Basic DateTime Format
To format the current timestamp for HTML5 datetime input −
<?php
echo date("Y-m-d\TH:i:s");
?>
2024-03-28T19:12:49
Converting Specific Timestamp
To convert a specific Unix timestamp to HTML5 datetime format −
<?php
$timestamp = 1640995200; // January 1, 2022 00:00:00
echo date("Y-m-d\TH:i:s", $timestamp);
?>
2022-01-01T00:00:00
HTML Integration
Using the formatted timestamp in an HTML datetime input element −
<input type="datetime-local" value="<?php echo date('Y-m-d\TH:i:s', $timestamp); ?>" />
Format Breakdown
| Format Code | Description | Example |
|---|---|---|
| Y | 4-digit year | 2024 |
| m | 2-digit month | 03 |
| d | 2-digit day | 28 |
| \T | Literal 'T' separator | T |
| H:i:s | Hour:Minute:Second | 19:12:49 |
Conclusion
Use date("Y-m-d\TH:i:s") to format PHP timestamps for HTML5 datetime inputs. The backslash before 'T' ensures it's treated as a literal character rather than a format code.
Advertisements
