Date and Time Formats | PHP - Hypertext Preprocessor

Standards Based Development

php's date function ( date()) allows the formatting of timestamps, so they are more human readable.

A timestamp is the number of seconds from 01 January 1970 at 00:00 hours. Otherwise known as the UNIX Timestamp.

date() uses letters of the alphabet to represent various parts of a typical date and time format.

Use the slash "/" inserted between the letters to add additional formatting.

March 9, 2016, 12:55 pm date("F j, Y, g:i a"); format
03.09.16 date("m.d.y"); format
9, 3, 2016 format
20160309 format
12-55-08, 9-03-16, 5531 5508 3 Wedpm16 68 format
it is the 9th day. format
Wed Mar 9 12:55:08 PST 2016 format
12:03:08 m is month format
12:55:08 format

php Date - Reference

Important Full Date and Time:

r: Displays the full date, time and timezone offset. It is equivalent to manually entering date("D, d M Y H:i:s O")

Time:

Day:

Month:

Year:

Other Formatting:

Display Start Date and Current Year

The example p below displays a start date and the current year:

© 2008-2016 Copyright.


<p>© <?php 
$copyYear = 2008; // Set your website start date
$curYear = date('Y'); // Keeps the second year updated
echo $copyYear . (($copyYear != $curYear) ? '-' . $curYear : '');
?> Copyright.</p>

Using php to Keep Copyright Footer Year Current

Pretty Date: Time Ago


<?php
function prettyDate($date){
  $time = strtotime($date);
  $now = time();
  $ago = $now - $time;
  if($ago < 60){
    $when = round($ago);
    $s = ($when == 1)?"second":"seconds";
    return "$when $s ago";
  }elseif($ago < 3600){
    $when = round($ago / 60);
    $m = ($when == 1)?"minute":"minutes";
    return "$when $m ago";
  }elseif($ago >= 3600 && $ago < 86400){
    $when = round($ago / 60 / 60);
    $h = ($when == 1)?"hour":"hours";
    return "$when $h ago";
  }elseif($ago >= 86400 && $ago < 2629743.83){
    $when = round($ago / 60 / 60 / 24);
    $d = ($when == 1)?"day":"days";
    return "$when $d ago";
  }elseif($ago >= 2629743.83 && $ago < 31556926){
    $when = round($ago / 60 / 60 / 24 / 30.4375);
    $m = ($when == 1)?"month":"months";
    return "$when $m ago";
  }else{
    $when = round($ago / 60 / 60 / 24 / 365);
    $y = ($when == 1)?"year":"years";
    return "$when $y ago";
  }
}
echo prettyDate("2012-07-22 12:23:45")."<br />";
echo prettyDate("2010-11-12 22:25:45")."<br />";
echo prettyDate("2012-01-01 01:00:00")."<br />";
echo prettyDate("2001-05-30 00:00:00")."<br />";
?>

Get Current Date Time

12:55 Wednesday 09 03 2016


echo date('H:i l d m Y')

Calculate Script Run Time

Script took 4.0531158447266E-6 seconds to execute.


<?php
//Create a variable for start time
$time_start = microtime(true);

// Place your PHP/HTML/JavaScript/CSS/Etc. Here

//Create a variable for end time
$time_end = microtime(true);
//Subtract the two times to get seconds
$time = $time_end - $time_start;

echo '<p>Script took <code>'.$time.'</cod> seconds to execute.</p>';
?>

Updating Copyright Year

© 2009-2016 companyname all rights reserved


<p>© 2009-
<?php echo date("Y"); ?>
companyname all rights reserved
</p>

RFC 822 Date and Time Script

RFC 822 Date and Time: Wed, 09 Mar 2016 12:55:08 -0800


function get_date_and_time()
{
  return date("r");	
}
echo get_date_and_time();

Date and Time (American) Script

Date and Time (American): 03/09/2016 12:55:08


function get_date_and_time() //MM/DD/YYYY HH:MM:SS
{
  return date("m/d/Y H:i:s");
} 
echo get_date_and_time();

Time and Date Syntax

Super basic date() function example:

Here is the date provided by the php date() function: 09-03-2016


// unnecessary markup
<p class="demop">Here is the date provided by the 
  <abbr>php</abbr>
  <code class="vphp">date()</code>
  <b>function</b>: 
  <strong>
  
// required php
<?php
  echo date('d-m-Y');
// date will print in "06-06-2026" format
?>

// more unnecessary markup
  </strong>
</p>

Format Characters

date()
Format CharacterDescriptionExample Returned Values
Day
d

Day of the month 2 digits

(01-31)
j

Day of the month

(1-31)
D

3 letter day

(Mon-Sun)
l

Full name of day

(Monday-Sunday)
N

1=Monday, 2=Tuesday, etc

(1-7)
S

Suffix for date

(st, nd, rd)
w

0=Sunday, 1=Monday

(0-6)
z

Day of the year

(1=365)
Week
W

Week of the year

(1-52)
Month
F

Full name of month

(January-December)
m

2 digit month number

(01-12)
n

Month number

(1-12)
M

3 letter month

(Jan-Dec)
t

Days in the month

(28-31)
Year
L

Leap year

(0 no, 1 yes)
o

ISO-8601 year number

Ex: (1979, 2006)
Y

Four digit year

Ex: (1979, 2006)
y

Two digit year

Ex: (79, 06)
Time
a

am or pm

(am or pm)
A

AM or PM

(AM or PM)
B

Swatch Internet time

(000-999)
g

12 hour

(1-12)
G

24 hour c

(0-23)
h

2 Digit 12 hour

(01-12)
H

2 Digit 24 hour

(00-23)
i

2 Digit minutes

(00-59)
s

0 2 Digit seconds

(00-59)
Other
e

Timezone

Ex: (GMT, CST)
I

Daylight savings

(1=yes, 0=no)
O

Offset GMT

Ex: (0200)
Z

Offset in seconds

(-43200-43200)
r

Full RFC 2822 formatted date

Date and Time in php