Skip to content

Australian Date Helper

World Wide Web Server edited this page Jul 4, 2012 · 7 revisions

Category:Helpers

This is a simple helper module I wrote to make working with Australian formatted dates a little easier. Provides functions for conversion of Unix date/time stamps to/from Australian style (d/m/y) strings.

Hope this is useful to others. With a bit of work it could become a geric date formatting/parsing helper, but it suits my needs for the time being.

[code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /* This File licensed under the terms of the BSD License:

Copyright (c) 2007, Tux IT Services All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this 
  list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, 
  this list of conditions and the following disclaimer in the documentation 
  and/or other materials provided with the distribution.
* Neither the name of the Tux IT Services nor the names of its contributors 
  may be used to endorse or promote products derived from this software 
  without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

/**

  • Australian Date Helper Module
  • Provides functions which assist in the conversion to/from Australian formatted dates.
  • @package Codeigniter
  • @subpackage Helpers
  • @category Helpers
  • @author Jim O'Halloran */

/**

  • Converts an australian (d/m/y) formatted string to a unix timestamp.
  • @param string Australian date string.
  • @return integer Unix timestamp. */ function au_date_to_unix($date_str) { $date_bits = explode('/', $date_str); if (count($date_bits) != 3) { return 0; } else { return mktime(0, 0, 0, $date_bits[1], $date_bits[0], $date_bits[2]); } }

/**

  • Converts a unix timstampto an australian formatted date string.
  • @param integer Unix timestamp
  • @return string Australian formatted date. */ function unix_date_to_au($unix_date) { return date('d/m/Y', $unix_date); }

/**

  • Converts an Australian formatted date/time string (d/m/y h:m:s) to a unix timestamp.
  • @param string An australian formatted date/time string
  • @return integer A unix timestamp. */ function au_datetime_to_unix($datetime_str) { $datetime_bits = explode(' ', $datetime_str); if (count($datetime_bits) != 2) { return 0; } else { $date_bits = explode('/', $datetime_bits[0]); if (count($date_bits) != 3) { return 0; } else { $time_bits = explode(':', $datetime_bits[1]); if (count($time_bits) != 2 && count($time_bits) != 3) { return 0; } else { if (count($time_bits) == 2) { $time_bits[2] = 0; } return mktime($time_bits[0], $time_bits[1], $time_bits[2], $date_bits[1], $date_bits[0], $date_bits[2]); } } } }

/**

  • Takes a unix timestamp and converts it to an australian formatted string including time.
  • @param integer Unix timestampo
  • @return string An australian formatted string. */ function unix_datetime_to_au($unix_datetime) { return date('d/m/Y H:i:s', $unix_datetime); }

/**

  • Accepts a short date (m/y) and returns a Unix timestamp representing midnight on the 1st of that month.
  • @param string A short Australian date.
  • @return integer A unix timestamp */ function au_short_date_to_unix($date_str) { $date_bits = explode('/', $date_str); if (count($date_bits) != 2) { return 0; } else { return mktime(0, 0, 0, $date_bits[0], 1, $date_bits[1]); } }

/**

  • Accepts a unix timestamp and returns an australian short (m/y) date string.
  • @param integer A unix timestamp
  • @return string A short date string. */ function unix_date_to_au_short($unix_date) { return date('m/Y', $unix_date); }

/**

  • Accepts a Unix timestamp and returns a string formatted ready for MySQL databases
  • containing only the date portion.
  • @param integer A Unix date stamp.
  • @return string A string ready for MySQL */ function unix_date_to_mysql($unix_date) { return date('Y-m-d', $unix_date); }

/**

  • Accepts a Unix timestamp and returns a string formatted ready for MySQL databases
  • containing both date and time portions.
  • @param integer A Unix date stamp.
  • @return string A string ready for MySQL */ function unix_datetime_to_mysql($unix_date) { return date('Y-m-d H;I;s', $unix_date); }

function start_of_day($unix_time) { $bits = getdate($unix_time); return mktime(0, 0, 0, $bits['mon'], $bits['mday'], $bits['year']); }

function end_of_day($unix_time) { $bits = getdate($unix_time); return mktime(23, 59, 59, $bits['mon'], $bits['mday'], $bits['year']); } [/code]

Note: If the BSD license isn't appropriate to your project, email the author at [email protected]. I'm happy to discuss alternative licensing.

Clone this wiki locally