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.
d: The day of the month. The type of expected output is01through31.m: The current month, as a number. Output expected is01through12.y: The current year in two digits (##). Output is00through99.
Use the slash "/" inserted between the letters to add additional formatting.
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:
a: am or pm depending on the timeA: AM or PM depending on the timeg: Hour without leading zeroes. Values are 1 through 12.G: Hour in 24-hour format without leading zeroes. Values are 0 through 23.h: Hour with leading zeroes. Values 01 through 12.H: Hour in 24-hour format with leading zeroes. Values 00 through 23.i: Minute with leading zeroes. Values 00 through 59.s: Seconds with leading zeroes. Values 00 through 59.
Day:
d: Day of the month with leading zeroes. Values are 01 through 31.j: Day of the month without leading zeroes. Values 1 through 31D: Day of the week abbreviations. Sun through Satl: Day of the week. Values Sunday through Saturdayw: Day of the week without leading zeroes. Values 0 through 6.z: Day of the year without leading zeroes. Values 0 through 365.
Month:
m: Month number with leading zeroes. Values 01 through 12n: Month number without leading zeroes. Values 1 through 12M: Abbreviation for the month. Values Jan through DecF: Normal month representation. Values January through December.t: The number of days in the month. Values 28 through 31.
Year:
L: 1 if it's a leap year and 0 if it isn't.Y: A four digit year formaty: A two digit year format. Values 00 through 99.
Other Formatting:
U: The number of seconds since the Unix Epoch (January 1, 1970)O: This represents the Timezone offset, which is the difference from Greenwich Meridian Time (GMT). 100 = 1 hour, -600 = -6 hours
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
- Pretty Date: 4 years ago from
2012-07-22 12:23:45 - Pretty Date: 5 years ago from
2010-11-12 22:25:45 - Pretty Date: 4 years ago from
2012-01-01 01:00:00 - Pretty Date: 15 years ago from
2001-05-30 00:00:00
<?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
| Format Character | Description | Example 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 | |