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 program to change date format
In PHP, you can change date formats using several built-in functions like date(), strtotime(), and date_create(). These functions allow you to convert dates between different formats and perform date calculations.
Using date() and strtotime()
The strtotime() function converts a date string to a timestamp, which can then be formatted using date() ?
<?php
$date = "2019-11-11 05:25 PM";
echo "Displaying date...<br>";
echo "Date = $date";
echo "\nDisplaying updated date...<br>";
echo date('Y-m-d H:i', strtotime($date. ' + 20 days'));
?>
Displaying date... Date = 2019-11-11 05:25 PM Displaying updated date... 2019-12-01 17:25
Using date_create() and date_format()
The date_create() function creates a DateTime object, which can be formatted using date_format() ?
<?php
$date = date_create("2019-11-11");
echo "Displaying date...<br>";
echo date_format($date, "Y/m/d H:i:s");
?>
Displaying date... 2019/11/11 00:00:00
Common Format Patterns
| Format Character | Description | Example |
|---|---|---|
| Y | 4-digit year | 2019 |
| m | Month (01-12) | 11 |
| d | Day (01-31) | 11 |
| H | Hour (00-23) | 17 |
| i | Minutes (00-59) | 25 |
Conclusion
PHP offers flexible date formatting through date() with strtotime() for simple conversions and date_create() with date_format() for object-oriented approaches. Both methods support extensive format patterns for customizing date output.
Advertisements
