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
Customize locale icons
Oct 29, 2024
By myq.

How to customize locale (language region) flags

Concrete CMS Caching Guide
Oct 16, 2024

An overview of types of caching in Concrete and considerations when using them.

Redirect all requests to HTTPS
Oct 9, 2024
By myq.

How to follow best practices for a secure web

Upgrade Concrete versions 9.3.1 and 9.3.2
Sep 10, 2024
By myq.

How to get past a bug in versions 9.3.1 and 9.3.2 that prevents upgrading the Concrete core through the Dashboard

How to use Composer with Marketplace extensions
Aug 22, 2024

Composer can be used to manage third-party extensions from the marketplace

Controlling Google Tag Manager Tags Based on Concrete CMS Edit Toolbar Visibility
Aug 13, 2024

This document provides a step-by-step guide on how to control the firing of Google Tag Manager (GTM) tags based on the visibility of the Concrete CMS edit toolbar. It explains how to create a custom JavaScript variable in GTM to detect whether the edit toolbar is present on a page and how to set up a trigger that ensures GTM tags only fire when the toolbar is not visible. This setup is particularly useful for developers and marketers who want to ensure that tracking and analytics tags are not activated during content editing sessions, thereby preserving the accuracy of data collected.

Improvements?

Let us know by posting here.