date_format


php apg

RETURNS date formatted according to given format.



<?php

/* - - - - - - - - - - - - - - - - -

    Object oriented style
   
   - - - - - - - - - - - - - - - - - */

str public DateTime::format str $format )


where,

$format The format STRING accepted by the function date

?>

<?php

/* - - - - - - - - - - - - - - - - -

    Object oriented style
   
   - - - - - - - - - - - - - - - - - */

str public DateTimeImmutable::format str $format )


where,

$format The format STRING accepted by the function date

?>

<?php

/* - - - - - - - - - - - - - - - - -

    Object oriented style
   
   - - - - - - - - - - - - - - - - - */

str public DateTimeInterface::format str $format )


where,

$format The format STRING accepted by the function date

?>

<?php

/* - - - - - - - - - - - - - - - - -

      Procedural style
   
   - - - - - - - - - - - - - - - - - */

str date_format DateTimeInterface $object str $format )


where,

$object DateTime object returned by the function date_create

$format 
The format STRING accepted by the function date

?>

 $format 

RECOGNIZED CHARACTERS
format character Description Example
returned values
Day
d Day of the month, 2 digits with leading zeros 01 to 31
D A textual representation of a day, three letters Mon through Sun
j Day of the month without leading zeros 1 to 31
l A full textual representation of the day of the week Sunday
through
Saturday
N ISO-8601 numeric representation of the day of the week
Since PHP 5.1.0
1 (for Monday)
through
7 (for Sunday)
S English ordinal suffix for the day of the month, 2 characters st, nd, rd or th.
Works well with j
w Numeric representation of the day of the week 0 (for Sunday)
through
6 (for Saturday)
z The day of the year (starting from 0) 0 through 365
Week
W ISO-8601 week number of year, weeks starting on Monday Example:
42
(the 42nd week in the year)
Month
F A full textual representation of a month, such as January or March January
through
December
m Numeric representation of a month, with leading zeros 01 through 12
M A short textual representation of a month, three letters Jan through Dec
n Numeric representation of a month, without leading zeros 1 through 12
t Number of days in the given month 28 through 31
Year
L Whether it's a leap year 1 if it is a leap year, 0 otherwise.
o ISO-8601 week-numbering year.
This has the same value as Y,
except that if the ISO week number (W) belongs to the previous or next year, that year is used instead.
Since PHP 5.1.0
Examples:
1999 or 2018
Y A full numeric representation of a year, 4 digits Examples:
1999 or 2018
y A two digit representation of a year Examples:
99 or 18
Time
a Lowercase Ante meridiem and Post meridiem am or pm
A Uppercase Ante meridiem and Post meridiem AM or PM
B Swatch Internet time 000 through 999
g 12-hour format of an hour without leading zeros 1 through 12
G 24-hour format of an hour without leading zeros 0 through 23
h 12-hour format of an hour with leading zeros 01 through 12
H 24-hour format of an hour with leading zeros 00 through 23
i Minutes with leading zeros 00 to 59
s Seconds, with leading zeros 00 through 59
u Microseconds - Since PHP 5.2.2.
Note that date_format will always generate 000000
since it takes an integer parameter,
whereas date_formatTime::format does
support microseconds if date_formatTime created with microseconds.
Example: 654321
v Milliseconds - Since PHP 7.0.0.
Same note applies as for u.
Example: 654
Timezone
e Timezone identifier
Since PHP 5.1.0
Examples: UTC, GMT, Atlantic/Azores
I (capital i) Whether or not the date_format is in Daylight Saving Time 1 if DST, 0 if not DST.
O Difference to Greenwich time (GMT) in hours Example: +0200
P Difference to Greenwich time (GMT)
with colon between hours and minutes.
Since PHP 5.1.3
Example: +02:00
p The same as P, but returns Z instead of +00:00 Example: +02:00
T Timezone abbreviation Examples: EST, MDT ...
Z Timezone offset in seconds.
The offset for timezones west of UTC
is always negative,
and for those east of UTC is always positive.
-43200 through 50400
Full date_format/Time
c ISO 8601 date_format (added in PHP 5) 2004-02-12T15:
:19:21+00:00
r RFC 2822 formatted date_format Example:
Thu, 21 Dec 2000 16:01:07 +0200
U Seconds since the Unix Epoch:
January 1 1970 00:00:00 GMT
See also the function time
ed48


 PREDEFINED DATE CONSTANTS 

FORMAT DATES
Usual Interface DateTimeInterface
STRING
DESCRIPTION
DATE_ATOM DateTimeInterface::ATOM
Y-m-d\TH:i:sP
Atom:
2018-08-15T15:52:01+00:00
DATE_COOKIE DateTimeInterface::COOKIE
l, d-M-Y H:i:s T
HTTP Cookies:
Monday, 15-Aug-2018 15:52:01 UTC
* DATE_ISO8601 DateTimeInterface::ISO8601
Y-m-d\TH:i:sO
ISO-8601:
2018-08-15T15:52:01+0000
DATE_RFC822 DateTimeInterface::RFC822
D, d M y H:i:s O
RFC 822:
Mon, 15 Aug 18 15:52:01 +0000
DATE_RFC850 DateTimeInterface::RFC850
l, d-M-y H:i:s T
RFC 850:
Monday, 15-Aug-18 15:52:01 UTC
DATE_RFC1036 DateTimeInterface::RFC1036
D, d M y H:i:s O
RFC 1036:
Mon, 15 Aug 18 15:52:01 +0000
DATE_RFC1123 DateTimeInterface::RFC1123
D, d M Y H:i:s O
RFC 1123:
Mon, 15 Aug 2018 15:52:01 +0000
DATE_RFC2822 DateTimeInterface::RFC2822
D, d M Y H:i:s O
RFC 2822:
Mon, 15 Aug 2018 15:52:01 +0000
DATE_RFC7231 DateTimeInterface::RFC7231
D, d M Y H:i:s \G\M\T
RFC 7231:
Mon, 15 Aug 2018 15:52:01 GMT
DATE_RFC3339 DateTimeInterface::RFC3339
Y-m-d\TH:i:sP
Same as DATE_ATOM
Since PHP 5.1.3
DATE_RFC3339_EXTENDED DateTimeInterface::RFC3339_EXTENDED
Y-m-d\TH:i:s.vP
RFC 3339 EXTENDED format
Since PHP 7.0.0
2018-08-15T15:52:01.000+00:00
DATE_RSS DateTimeInterface::RSS
D, d M Y H:i:s O
RSS:
Mon, 15 Aug 2018 15:52:01 +0000
DATE_W3C DateTimeInterface::W3C
Y-m-d\TH:i:sP
World Wide Web Consortium:
2018-08-15T15:52:01+00:00
ed48

* This format is not compatible with ISO-8601, but is left this way for backward compatibility reasons.

Use DATE_ATOM or DateTimeInterface::ATOM for compatibility with ISO-8601 instead.



  1 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -
   For each new run - 
   - a new result will be shown
   - - - - - - - - - - - - - - - - - - - - - - - */

$ddate01 = new DateTime("America/Sao_Paulo");

echo 
$ddate01->format('(U) [O] d-M-Y H:i:s');

?> 

  2 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -
   For each new run - 
   - a new result will be shown
   - - - - - - - - - - - - - - - - - - - - - - - */

$ddate02 = new DateTime(date('c'time()));

echo 
'( ' $ddate02->format('B') . ' beat )<br>';
echo 
$ddate02->format('(U) => T d-M-Y H:i:s');

?> 

  3 EXERCISE   

<?php

$ddate03 
= new DateTime(date('c''1234567890'));

echo 
'( ' $ddate03->format('B') . ' beat )<br>';
echo 
$ddate03->format('(U) => T d-M-Y H:i:s');

?> 

  4 EXERCISE   

<?php

date_default_timezone_set
("UTC");

$ddate040 = new DateTime(date('c''-7200'));

echo 
$ddate040->format('B') . ' beat = ';
echo 
$ddate040->format('(U) => T d-M-Y H:i:s');
echo 
'<br>PRIOR TO THE BEGINNING OF UNIX ERA<br><br>';

$ddate041 = new DateTime(date('c''0'));

echo 
$ddate041->format('B') . ' beat = ';
echo 
$ddate041->format('(U) => T d-M-Y H:i:s');
echo 
'<br>BEGINNING OF THE UNIX ERA<br><br>';

$ddate042 = new DateTime(date('c''2147483647'));

echo 
$ddate042->format('B') . ' beat = ';
echo 
$ddate042->format('(U) => T d-M-Y H:i:s');
echo 
'<br>END OF THE 32 bit UNIX ERA<br><br>';


$ddate043 = new DateTime(date('c''146011735807'));

echo 
$ddate043->format('B') . ' beat = ';
echo 
$ddate043->format('(U) => T d-M-Y H:i:s');
echo 
'<br>AFTER THE END OF THE 32-BIT UNIX ERA<br>
                    WITHIN THE 64 BIT ENVIRONMENT.<br><br>'
;

?>  

  5 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -
   For each new run - 
   - a new result will be shown
   - - - - - - - - - - - - - - - - - - - - - - - */

date_default_timezone_set("UTC");

$ddate050 = new DateTime(date('c'time()));

echo 
$ddate050->format(DATE_RFC3339_EXTENDED);
echo 
'<br><br>Actual date and time from UTC<br><br>';

?>  

  6 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -
   For each new run - 
   - a new result will be shown
   - - - - - - - - - - - - - - - - - - - - - - - */

date_default_timezone_set("UTC");

echo 
'Actual date and time from UTC:<br><br>';

$ddate060 = new DateTime(date('c'time()));

echo 
$ddate060->format('U => T d-M-Y H:i:s') . '<br><br>';

echo 
'Timestamp = ' $ddate060->getTimestamp() . '<br><br>';

echo 
'offSet = ' $ddate060->getOffset() . '<br><br>';

$arr060 = ((array) $ddate060->getTimezone() );

foreach (
$arr060 as $k060 => $v060)
{
echo 
$k060 ': ' $v060 '<br><br>';


date_default_timezone_set("America/Sao_Paulo");

echo 
'<br><br>Actual date and time from America/Sao_Paulo:<br><br>';

$ddate061 = new DateTime(date('c'time()));

echo 
$ddate061->format('U => T d-M-Y H:i:s') . '<br><br>';

echo 
'Timestamp = ' $ddate061->getTimestamp() . '<br><br>';

echo 
'offSet = ' $ddate061->getOffset() . '<br><br>';

$arr061 = ((array) $ddate061->getTimezone() );

foreach (
$arr061 as $k061 => $v061)
{
echo 
$k061 ': ' $v061 '<br><br>';



?>  

  7 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -
   For each new run - 
   - a new result will be shown
   - - - - - - - - - - - - - - - - - - - - - - - */

date_default_timezone_set('Asia/Tokyo');

echo 
'Actual date and time from Asia/Tokyo:<br><br>';

$ddate071 = new DateTime(date('c'time()));

echo 
$ddate071->format("Y-m-d\ T H:i:sP") . '<br><br>';

echo 
'Timestamp = ' $ddate071->getTimestamp() . '<br><br>';

echo 
'offSet = ' $ddate071->getOffset() . '<br><br>';

$arr071 = ((array) $ddate071->getTimezone() );

foreach (
$arr071 as $k071 => $v071)
{
echo 
$k071 ': ' $v071 '<br><br>';
}
 
/*
echo 'Timezone = ';
var_dump($ddate071->getTimezone());
echo '<br><br>';
*/

?>  

  8 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -
   For each new run - 
   - a new result will be shown
   - - - - - - - - - - - - - - - - - - - - - - - */

$tzo08 timezone_open('America/Los_Angeles');

$ntz08 timezone_name_get($tzo08);

date_default_timezone_set($ntz08);

echo 
'Actual date and time from: ' $ntz08 '<br><br>';

$date08 date_create();

$datef08 date_format($date08DateTimeInterface::ATOM);

echo 
$datef08 '<br>';

?>  

  9 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -
   For each new run - 
   - a new result will be shown
   - - - - - - - - - - - - - - - - - - - - - - - */

$tz09 DateTimeZone::listIdentifiers(4095);
// The all time zones name list

$rnd09 mt_rand(0count($tz09) - 1);

$tzn09 $tz09[$rnd09];

$tzo09 timezone_open($tzn09);

$ntz09 timezone_name_get($tzo09);

date_default_timezone_set($ntz09);

echo 
'Actual date and time from: ' $ntz09 '<br><br>';

$ddate091 = new DateTimeImmutable("now"); 

echo 
$ddate091->format("Y-m-d\ T H:i:sP") . '<br><br>'

echo 
'Timestamp = ' $ddate091->getTimestamp() . '<br><br>'

echo 
'offSet = ' $ddate091->getOffset() . '<br><br>';

$arr091 = ((array) $ddate091->getTimezone() );

foreach (
$arr091 as $k091 => $v091)
{
echo 
$k091 ': ' $v091 '<br><br>';
}  

?>  

  10 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -
   For each new run - 
   - a new result will be shown
   - - - - - - - - - - - - - - - - - - - - - - - */

$ntz10 date_default_timezone_get();

echo 
'Actual date and time from: ' $ntz10 '<br><br>';

$ddate101 = new DateTime("now"); 

echo 
$ddate101->format("Y-m-d\ T H:i:sP") . '<br><br>'

echo 
'Timestamp = ' $ddate101->getTimestamp() . '<br><br>'

echo 
'offSet = ' $ddate101->getOffset() . '<br><br>'

echo 
'Timezone = '
var_dump($ddate101->getTimezone()); 
echo 
'<br><br>'

?>  

  11 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -
   For each new run - 
   - a new result will be shown
   - - - - - - - - - - - - - - - - - - - - - - - */

//Set the default time zone
date_default_timezone_set("Europe/London");

echo 
"Testing date_format() : basic functionality.<br><br><br>";
$date date_create("now");

echo( 
date_format($date"F j, Y, g:i a") );
echo 
'<br><br>';
echo( 
date_format($date"m.d.y") );
echo 
'<br><br>';
echo( 
date_format($date"j, n, Y") );
echo 
'<br><br>';
echo( 
date_format($date"Ymd") );
echo 
'<br><br>';
echo( 
date_format($date'h-i-s, j-m-y, it is w Day') );
echo 
'<br><br>';
echo( 
date_format($date'\i\t \i\s \t\h\e jS \d\a\y.') );
echo 
'<br><br>';
echo( 
date_format($date"D M j G:i:s T Y") );
echo 
'<br><br>';
echo( 
date_format($date'H:m:s  m \= M \i\s\ \m\o\n\t\h') );
echo 
'<br><br>';
echo( 
date_format($date"H:i:s") );
echo 
'<br><br>';
echo( 
date_format($date"B") ) . ' beat';

?>  

  12 EXERCISE   

<?php

$tz 
= array("UTC""Europe/London"
                   
"Europe/Berlin""America/Chicago");

foreach (
$tz as $zone) {
    echo 
$zone"<br>";
    
date_default_timezone_set($zone);

    
$date date_create("now");

    echo( 
date_format($date"O") );
    echo 
'<br><br>';
    echo( 
date_format($date"P") );
    echo 
'<br><br>';
    echo( 
date_format($date"p") );
    echo 
'<br><br>';
}

echo 
"Z<br>";
$date date_create("now");

echo( 
date_format($date"p") );

?>