Skip to content

rollinsw/CakePHP-Date-Format-Helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 

Repository files navigation

CakePHP-Date-Format-Helper

A CakePHP helper to format dates consistently throughout an application

Description

This is a simple helper that allows you to consistently format dates throughout your application. If someone suddenly decides that dates should look like 1/1/2013 instead of 01-01-2013, you will just need to change it in one place. It includes options to display either the date part or time part or both. It also includes the option to pass in a time offset for dealing with timezones.

I created this when working on a project where the client changed their mind several times about how dates should be formatted on the site. This allowed me to easily make this change in one place.

Requirements

CakePHP 2.x

Installation

Copy the DateFormatHelper.php file to /app/View/Helper

Setup

Edit the following variable in DateFormatHelper.php to adjust the helper defaults:

  • dateOnlyFormat: PHP date format options to use when only date is displayed
  • timeOnlyFormat: PHP date format options to use when only time is displayed
  • dateTimeFormat: PHP date format options to use when both date and time are displayed
  • defaultDatePartDisplay: True/False default setting for date part display
  • defaultTimePartDisplay: True/False default setting for time part display
  • defaultOffset: Default value for time offset in hours

If you want the Date Format helper available to all controllers, enable it using the $helpers property in /app/Controller/AppController.php:

public $helpers = array('DateFormat');

You might have other Helpers already enabled in the helpers array. If so, add the DateFormat helper to the array:

public $helpers = array('Html', 'Session', 'Form', 'Text', 'DateFormat');

Usage

Use the following to format a date when displaying in a view: echo $this->DateFormat->formatDate($dateString, $options); where $dateString is your date as a string and $options is an array that contains these options:

  • displayDatePart: True/False display the date portion
  • displayTimePart: True/False display the time portion
  • offset: An offset for the date in hours. Intended to be used to adjust date/time based on timezones.


$myDate = '2013-01-21 15:56:41';

// Would echo something like: 1/21/2013
echo $this->DateFormat->formatDate($myDate, array(
	'displayDatePart'=>true, 
	'displayTimePart'=>false, 
	'offset'=>-5)
);

// Would echo something like: 1/21/2013 10:56
echo $this->DateFormat->formatDate($myDate, array(
	'displayDatePart'=>true, 
	'displayTimePart'=>true, 
	'offset'=>-5)
);

License

Copyright (c) 2013 William Rollins

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A CakePHP helper to format dates consistently throughout an application

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published