Filter by specific date, month and day and exclude min and seconds on List Page

5 posts by 2 authors in: Forums > CMS Builder
Last Post: August 4, 2013   (RSS)

By gkornbluth - August 3, 2013

Hi nmsinc,

Here's a recipe from my CMSB Cookbook http://www.thecmsbcookbook.com that deals with this. Hope it hel[s,

Jerry Kornbluth

SHOW DATES ON YOUR PAGE - Nov 29th, 2010


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

_____ code ________________________________________________

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

__________________________________________________________

to your page.

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

_____ code ________________________________________________

<?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:

_____ code ________________________________________________


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

__________________________________________________________

Or, on a list page:

_____ code ________________________________________________

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

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

_____ code ________________________________________________

<?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:

_____ code ________________________________________________

<?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 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

By nmsinc - August 4, 2013

Thanks Jerry - Your last post on dates helped and I was able solve the issue in fine time. Thanks again!

nmsinc

nmsinc

By gkornbluth - August 4, 2013

Hi nmsinc,

Glad you got it to work.

Can we learn something from your final solution?

Jerry

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

By nmsinc - August 4, 2013

Hi Jerry,

I coded a daily print schedule for Zick's calendar. The  Zick calendar stores date and time and I needed to list all reminders for a specific day discarding the time factor. Adding the month, day & year filter against 'date' field returned nothing due to the absence of the time factor of the field

I looked over your date code and suggestions and came up with an idea on how to solve this issue. I added another date field that saves only the month, year and day of the reminder. Now I can filter by the added field and everything works just great!

It may not be the way the pros at I.T. would have solved this issue, however it worked for me!

Thanks again for help on this issue Jerry - keep up the good work :)

nmsinc.

nmsinc