Professional Documents
Culture Documents
Documentation
Get Involved
Help
Search
Getting Started
Introduction
A simple tutorial
Language Reference
Basic syntax
Types
Variables
Constants
Expressions
Operators
Control Structures
Functions
Classes and Objects
Namespaces
Enumerations
Errors
Exceptions
Fibers
Generators
Attributes
References Explained
Predefined Variables
Predefined Exceptions
Predefined Interfaces and Classes
Context options and parameters
Supported Protocols and Wrappers
Security
Introduction
General considerations
Installed as CGI binary
Installed as an Apache module
Session Security
Filesystem Security
Database Security
Error Reporting
User Submitted Data
Hiding PHP
Keeping Current
Features
HTTP authentication with PHP
Cookies
Sessions
Dealing with XForms
Handling file uploads
Using remote files
Connection handling
Persistent Database Connections
Command line usage
Garbage Collection
DTrace Dynamic Tracing
Function Reference
Affecting PHP's Behaviour
Audio Formats Manipulation
Authentication Services
Command Line Specific Extensions
Compression and Archive Extensions
Cryptography Extensions
Database Extensions
Date and Time Related Extensions
File System Related Extensions
Human Language and Character Encoding Support
Image Processing and Generation
Mail Related Extensions
Mathematical Extensions
Non-Text MIME Output
Process Control Extensions
Other Basic Extensions
Other Services
Search Engine Extensions
Server Specific Extensions
Session Extensions
Text Processing
Variable and Type Related Extensions
Web Services
Windows Only Extensions
XML Manipulation
GUI Extensions
Keyboard Shortcuts
?
This help
j
Next menu item
k
Previous menu item
gp
Previous man page
gn
Next man page
G
Scroll to bottom
gg
Scroll to top
gh
Goto homepage
gs
Goto search
(current page)
/
Focus search box
DateTime::getOffset »
« DateTime::diff
PHP Manual
Function Reference
Date and Time Related Extensions
Date/Time
DateTimeInterface
Change language:
English
Submit a Pull Request
Report a Bug
DateTime::format
DateTimeImmutable::format
DateTimeInterface::format
date_format
(PHP 5 >= 5.2.1, PHP 7, PHP 8)
Description ¶
Object-oriented style
Procedural style
Parameters ¶
object
Note:
Return Values ¶
Returns the formatted date string on success.
Changelog ¶
Version Description
8.0.0 Prior to this version, false was returned on failure.
8.0.0 The format character p has been added.
Examples ¶
Object-oriented style
<?php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
?>
Procedural style
<?php
$date = date_create('2000-01-01');
echo date_format($date, 'Y-m-d H:i:s');
?>
2000-01-01 00:00:00
Notes ¶
See Also ¶
add a note
<?php
$row = mysql_fetch_row($result);
$date = date_create($row[0]);
#output: 24/03/12
#output: 5:45 PM
#output: 05:45pm
?>
up
down
35
soul dot enforcer at gmail dot com ¶
8 years ago
For full reference of the supported format character and results,
http://www.php.net/manual/en/function.date.php
up
down
15
sparcbr at gmail dot com ¶
1 year ago
To add literal characteres youi can escape with backslash (\):
echo $date->format('Ymd\Thms');
up
down
15
mesa dot fx at gmail dot com ¶
3 years ago
There is a bit confusing logic may appear using year week number:
<?php
?>
will output 201801, not 201901 nor 201852, because of strange ISO_8601-2004 standard: the first
calendar week of a year is that one which includes the first Thursday of that year, so
this date (it is Monday) do belong to the first week of 2019 (this is why 'W' format gives 01), but
internal timestamp is of 2018 (and 'Y' format obey this), therefore getting us unexpected result of
201801. So be careful when using this output with something important (i know projects where this was
used to form MySQL partitions).
up
down
33
daysnine at gmail dot com ¶
8 years ago
Seems like datetime::format does not really support microseconds as the documentation under date
suggest it will.
if (is_null($utimestamp))
$utimestamp = microtime(true);
$timestamp = floor($utimestamp);
}
I have a date that looks like this: 'November 3, 2020 11:13 (CST)'
What *did* work was the format string without the parentheses: 'F j, Y H:i T'.
This string also parsed the timezone when the time zone *didn't* have parentheses surrounding it:
'November 3, 2020 11:13 CST'.
It seems as though the T token is a little greedy with surrounding parentheses, so don't include them
in your format string.
up
down
-15
ca at agercon dot dk ¶
10 years ago
The date_format can be use to get the last day of February:
<?php
# The 0th day of a month is the same as the last day of the month before
$ultimo_feb_date = date_create($ultimo_feb_str);
return $return;
?>
up
down
-32
prussell at cloudworksconsulting dot com ¶
7 years ago
The udate function is a great start, but the formatting of the milliseconds is a little off. If it is
within the first 100000 microseconds then the string will be less than 6 characters, so 0.012435 will
appear as 0.12345. The revision below fixes this.
if (is_null($uTimeStamp))
{
$uTimeStamp = microtime(true);
}
$dtTimeStamp = floor($uTimeStamp);
// Then use the date function to process the rest of the string
}
up
down
-19
info at ibusweb dot com ¶
3 years ago
$saved_time="2019-03-09 14:25:20";
$formated_saved_time = new DateTime($saved_time);
$interval = $current_time->diff($formated_saved_time);
if (!empty($interval->format('%a'))){
$time_difference="yesterday";
}elseif (!empty($interval->format('%h'))){
} elseif (!empty($interval->format('%i'))){
} elseif (!empty($interval->format('%s'))){
}
https://eval.in/1081921
up
down
-55
chris at codewiz dot biz ¶
8 years ago
I believe this is a bug but its note-worthy if it is intended (I am using PHP 5.5.3).
$ php --version
What is happening:
DateTime()->format() will modify the timezone. So do not expect the public date property to be
returned (format mask applied) based on the current public timezone property. format will decide that
when calling ->format() it will use the server timezone which eliminates all usefulness of -
>setTimezone().
<?php
echo '$nowUtc'.PHP_EOL;
var_dump($nowUtc);
var_dump($nowUtc->format('Y-m-d h:i:s'));
var_dump($nowUtc);
var_dump($nowUtc->format('Y-m-d h:i:s'));exit;
?>
outputs;
$nowUtc
object(DateTime)[2607]
$nowUtc->format('Y-m-d h:i:s')
object(DateTime)[2608]
$nowUtc->format('Y-m-d h:i:s')
DateTimeInterface
diff
format
getOffset
getTimestamp
getTimezone
__wakeup