SHOW DATES ON YOUR PAGE - Jan 15th, 2015


Sometimes you’ll want to show the current year on your page, like when showing copyright information.
It’s easy to show the current year by adding



<?php echo date("Y") ?>



to your page.

You can use any of the date modifiers to show date information as well, so:



<?php echo date("D, M jS, Y") ?>



Would show the Day of the week, and the Month, Day and Year.

You can format a date string to display as Day Date, Month Year on a detail page by using:




<?php echo date("D jS, M Y ", strtotime($your_tableRecord['your_date_ field'])) ?>



Or, on a list page:



<?php echo date("D jS, M Y ", strtotime($record['your_date_field'])) ?>



If you thought, as I did, that you couldn’t format the displaying of the special fields createdDate and updatedDate,
you’d be wrong.

You can format these the same as any other date field.

NOTE: The commas in the string determine where commas show in your display.

This is the complete list of Date formatting modifiers

A = Uppercase 12hr time syntax - Example: AM, PM
a = Lowercase 12hr time syntax - Example: am, pm
B = Swatch Internet time - Example: 000 through 999
c = ISO 8601 date (added in PHP 5) - Example: 2004-02-12T15:19:21+00:00
D = short textual representation of the Day - Example: Fri
d = Day of the month, 2 digits with leading zeros - Example: 03
e = Timezone identifier (added in PHP 5.1.0) - Example: UTC, GMT, Atlantic/Azores
F = Full Textual representation of the month - Example: January
G = Hours in 24 hour format without leading zeros - Example: 17
g = Hours in 12 hour format without leading zeros - Example: 7
H = Hours in 24 hour format with leading zeros - Example: 17
h = Hours in 12 hour format with leading zeros - Example: 07
I (capital i) = Whether or not the date is in daylight saving time - Example: 1 if Daylight Saving Time, 0 otherwise
i = Minutes with leading zeros - Example: 00 through 59
j = Day of the month without leading zeros - Example: 10, 25
L = Whether it's a leap year - Example: 1 if it is a leap year, 0 otherwise
l (lowercase 'L') = full textual representation of the day of the week e.g. Monday, Tuesday
M = A short textual representation of a month, three letters - Example: Jan, Feb
m = Numeric representation of a month, with leading zeros - Example: 01
N = ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) - Example: 1 (for Monday) through 7 (for
Sunday)
n = Numeric representation of a month, without leading zeros - Example: 1 through 12
O = Difference to Greenwich time (GMT) in hours - Example: +0400
o = ISO-8601 year number. 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. (added in PHP 5.1.0) e.g. 2010
P = Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3) - Example: +02:00
r = RFC 2822 formatted date - Example: Thu, 21 Dec 2000 16:01:07 +0200
S = English ordinal suffix for the day of month - Example: st, nd, rd, or th.
s = Seconds with leading zeros - Example: 28
T = Timezone abbreviation - Example: GMT, PST, EST, etc
t = Number of days in the given month - Example: 28, 31, etc.
U = Seconds since the UNIX epoch (January 1 1970 00:00:00 GMT) - Example: 1041604168
u = Microseconds (added in PHP 5.2.2) - Example: 654321
W = ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) - Example: 42 (the 42nd week in the
year)
w = numeric representation of the day - Example: 0 (for Sunday) through 6 (for Saturday)
Y = 4 digit year - Example: 2003
y = 2 digit year - Example: 03
Z = Timezone offset in seconds. The offset for time zones west of UTC is always negative, and for those east of UTC is
always positive. - Example: 43200 through 50400
z = Day of the year (starting from 0) - Example: 0 through 365

And here are some Date String examples and their output

date("F j, Y, g:i a"); - March 10, 2010, 5:16 pm
date("m.d.y"); - 03.10.10
date("j, n, Y") - 10, 3, 2010
date("D M j G:i:s T Y"); - Sat Mar 10 15:16:08 PST 2010
date("D M jS, Y"); - Sat Mar 10th, 2010
date("H:i:s"); - 17:16:17
date('\i\t \i\s \t\h\e jS \d\a\y.'); - It is the 10th day.

*** According to Dave Edis of Interactive Tools, in V2.01+ if you have a blank date field, the date stored in your
database is actually 0000-00-00 00:00:00, which is the default format MySQL stores dates in.

So, if you try to display the date, the technical reason you get 1969 or 1970 is because the way many servers record
time is as epoch time or "seconds since midnight 1970 GMT", so what strtotime() does is covert your date to epoch time,
but since your date is zeroed out you get 0 seconds since 1970, which the date() function interprets as 1970, and then
adjusts for your timezone (GMT -8 hours?)

If you try to test for a blank date with a simple:



<?php if ($your_tableRecord['your_date_field']): ?>



The test won’t work as you expect, since there is actually a date value stored in your database, (0000-00-00
00:00:00)

Until this glitch is fixed, and you update to the “fixed” version, you’ll have to format your test like this:



<?php if ($your_tableRecord['your_date_field'] && $your_tableRecord['your_date_field'] != '0000-00-00 00:00:00'): ?>



You can learn more about date formatting at:

http://www.php.net/date



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service