How to use the date time widget in concrete5 5.7

This is a community-contributed tutorial. This tutorial is over a year old and may not apply to your version of Concrete CMS.
Apr 27, 2015

The date time widget has three methods.
date()
datetime()
translate()

Core::make('helper/form/date_time')

date( string $field, string $value = null, boolean $calendarAutoStart = true )

datetime( string $prefix, string $value = null, boolean $includeActivation = false, boolean $calendarAutoStart = true )

translate( string $field, array $arr = null )

http://documentation.concrete5.org/api/class-Concrete.Core.Form.Service.Widget.DateTime.html

in form.php:

Date Only Picker:

Core::make('helper/form/date_time')->date('date_only', $date_only);

Using the date time widget to get a date picker using the date() method.

date('field', $value)

This creates the jQuery UI date picker. The date value saved is in "Y-m-d" format.

Date and Time Picker

Core::make('helper/form/date_time')->datetime('date_and_time', $date_and_time);

Using the date time widget to get a date and time picker using the datetime() method.

datetime('field', $value)

In addition to the jQuery UI date picker, there will be a select drop down for hours, minutes, and AM/PM. The date/time value saved is in "Y-m-d H:i:s" MySQL timestamp format.

When using the datetime() method, it creates multiple fields that must be combined using the translate() method. The save() method must be extended in the block controller to use translate().

in controller.php

public function save($args)
{
    $args['date_and_time'] = Core::make('helper/form/date_time')->translate('date_and_time');
    parent::save($args);
}

The save() method is extended to use the translate() method. The $args paramenter is the POST superglobal array. The date/time picker returns values from multiple fields - date, hour, minutes, and AM/PM. The different date/time fields need to be combined (translated) into a timestamp (combined into one value) to be saved. Translate() combines the different date/time fields from the POST superglobal array and translates (combines) them into a timestamp.

in db.xml


The field type for date and date/time is "T".
T: Datetime or Timestamp accurate to the second

http://www.concrete5.org/documentation/how-tos/creating-and-working-with-db-xml-files/

Recent Tutorials
Upgrading Concrete from 8.5 to 9.x
Jun 21, 2024
By myq.

How to avoid problems upgrading from 8.5 to 9.x

How to change the default date format
May 30, 2024
By myq.

Change the format of the default date

WebOps Tutorial on Running and Upgrading a Concrete CMS Website
May 2, 2024
By myq.

Describes how to deploy, manage, and upgrade a Concrete CMS website

Using the Concrete Migration Tool Addon
Apr 27, 2024

How to use the Concrete CMS Migration Tool

Create custom Site Health tasks
Apr 19, 2024
By myq.

This tutorial will guide you through the creation of a new Site Health task

Reusing the same Express entity in multiple associations
Apr 11, 2024
By myq.

How to create and manage multiple associations in Express

Improvements?

Let us know by posting here.