Date field

4 posts by 2 authors in: Forums > CMS Builder
Last Post: November 13, 2011   (RSS)

Hello,

Is it possible to create a "date only" field rather than a "date and time" field in cmsAdmin?

Thank you,
Greg

Re: [gversion] Date field

Hi Greg,

Absolutely, and it's easy.

Here's a portion of a recipe from the "Working With Dates" section of my CMSB Cookbook http://www.thecmsbcookbook.com that tells you how.

SHOWING DATES ON YOUR PAGE

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, 20101
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?)

Hope that helps to get you going.

Best,

Jerry Kornbluth
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Date field

Hi Jerry,

Thanks for the quick reply.

I'm still not sure though how to change the type of date field when creating and editing a field in the cmsAdmin area. I think your previous message just explains how to change the way a date/time field is displayed at the front-end (either a detail or a list page).

Is it possible to hide the "hours", "minutes", "seconds" drop down menus from the date/time field? I don't want users to be able to select a time - they should only be able to select a dd-mm-yyyy.

Thanks again.

Greg