PostNuke: A Flexible Open Source Content Management System
home | forum | international support | contact us

Documentation Wiki

Manipulating Date Values


Dates can be slightly difficult to handle correct all the way from the database to the UI and back again, so here's a short introduction to how it should be done.

As an example we can use a course module that lets you add and edit courses with starting and ending dates (startDate and endDate database fields).

Creating Date Fields in the Database


Please read ManipulatingTables first. Your ADODB data dictionary lines for the startDate and endDate fields should be:

<?php

  $sql = "...
          startDate T,
          endDate T,
          ..."
;

    if (!DBUtil::createTable('courses', $sql)) { ... }
   
?>


The 'T' type specifier results in an ~MySQL DATETIME column.

Fetching Dates from the database


The simplest way to fetch a database record is to use the DBUtil::selectObjectByID function:

<?php

  $course = DBUtil::selectObjectByID('course', $id);
 
?>


That is all! The $course variable will now contain your date values formatted as a 'YYYY-DD-MM HH:MM:SS' string which can be reformatted later on using various utility functions.

Remember: dates are always manipulated as strings in the format 'YYYY-DD-MM HH:MM:SS'.

Displaying Dates


Dates should be displayed like any other value in your pnRender template. The PHP code to do this would be:

<?php

  $course = DBUtil::selectObjectByID('course', $id);
  $render = new pnRender('course');
  $render->assign('course', $course);

  return $render->fetch('course_user_view.html');
 
?>


The template should be something like this:

  Course period: <!--[dateformat datetime=$course.startDate]--> - <!--[dateformat datetime=$course.endDate]-->


The dateformat plugin will reformat your date value according to it's default date format specifier (unspecified so far). If you want a different format you can supply that yourself:

  Course period: <!--[dateformat datetime=$course.startDate format="m-d-Y"]--> - <!--[dateformat datetime=$course.endDate]-->


The format used is that of PHP's date() function.

Empty Date Values


If you have allowed SQL NULL values in your date columns you will get PHP null values back when calling DBUtil::selectObjectByID. These are displayed as a blank values when using the dateformat plugin.

Entering Date Value


There's a special pncalendarinput plugin to be used for entering date values:

  <!--[pncalendarinit]-->

  Please enter start date: <!--[pncalendarinput ...]-->


To use it you must remember to put <!--[pncalendarinit]--> somewhere one the page before it. This will ensure that the proper javascript and stylesheets are loaded for the nice little calendar date picker you will get. This plugin does not add more than a calendar popup feature - you will have to add an <input> element for your self.

So far this is all you get. At the time of writing there is (to my knowledge) no dedicated features for reading dates back into the database. You will have to do date validation your self. You will also have to ensure the dates are in MySQL format before submitting them.



CategoryDeveloperDocs
XML Revisions of $tag
Page history :: Last Editor [ Chestnut ] :: Owner [ JørnLind ] ::
Valid XHTML :: Valid CSS :: Powered by pnWikka 1.0 (A wiki fork from WikkaWiki)
 

Main Menu

Extensions Database

Documentation

Development

Login





 


 Log in Problems?
 New User? Sign Up!

Donate to PostNuke