PHP date_format() Function
This tutorial is from Rookie Tutorial.
Definition and Usage
The date_format() function returns a date formatted according to the specified format.
Note: This function does not use the locale setting (uselocale). Use strftime() to format dates according to locale.
Syntax
date_format(object, format)
Parameter Values
| Parameter | Description |
|---|---|
| object | Required. Specifies a DateTime object returned by date_create() |
| format | Required. Specifies the format for the date. Uses the same format as the date() function. |
Technical Details
- Return Value: Returns the formatted date string on success, or FALSE on failure.
- PHP Version: 5.2+
Example 1
Format a date:
<?php
$date=date_create("2013-03-15");
echo date_format($date,"Y/m/d");
?>
Example 2
Format a date using a different format:
<?php
$date=date_create("2013-03-15");
echo date_format($date,"Y-m-d H:i:s");
?>
Example 3
Format a date using a custom pattern:
<?php
$date=date_create("2013-03-15");
echo date_format($date,"l jS \of F Y");
?>
Complete PHP Date Reference
For a complete reference of all PHP date functions, visit our PHP Date Reference.
Note: This page is a translation of the original Chinese tutorial from Rookie Tutorial.
YouTip