diff options
author | Eric Barnes <eric@ericlbarnes.com> | 2011-08-29 05:59:15 +0200 |
---|---|---|
committer | Eric Barnes <eric@ericlbarnes.com> | 2011-08-29 05:59:15 +0200 |
commit | 65faf9fb927cd20bc0b98f0f33ae662a2611c6de (patch) | |
tree | ed0677704edeb364ed17080b8c993e894550c024 /system/helpers/date_helper.php | |
parent | 91249f1154c6cc078afbf23faa60b9f6414ab597 (diff) | |
parent | 4f17de94737f5eb9e1230f7d854dea611ebd9901 (diff) |
Merge branch 'develop' into feature/unit-tests
Diffstat (limited to 'system/helpers/date_helper.php')
-rw-r--r-- | system/helpers/date_helper.php | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/system/helpers/date_helper.php b/system/helpers/date_helper.php index ce37ad9a3..f1febfa1a 100644 --- a/system/helpers/date_helper.php +++ b/system/helpers/date_helper.php @@ -492,6 +492,72 @@ if ( ! function_exists('human_to_unix')) // ------------------------------------------------------------------------ /** + * Turns many "reasonably-date-like" strings into something + * that is actually useful. This only works for dates after unix epoch. + * + * @access public + * @param string The terribly formatted date-like string + * @param string Date format to return (same as php date function) + * @return string + */ +if ( ! function_exists('nice_date')) +{ + function nice_date($bad_date='', $format=false) + { + if (empty($bad_date)) + { + return 'Unknown'; + } + // Date like: YYYYMM + if (preg_match('/^\d{6}$/',$bad_date)) + { + //echo $bad_date." "; + if (in_array(substr($bad_date, 0, 2),array('19', '20'))) + { + $year = substr($bad_date, 0, 4); + $month = substr($bad_date, 4, 2); + } + else + { + $month = substr($bad_date, 0, 2); + $year = substr($bad_date, 2, 4); + } + return date($format, strtotime($year . '-' . $month . '-01')); + + } + + // Date Like: YYYYMMDD + if (preg_match('/^\d{8}$/',$bad_date)) + { + $month = substr($bad_date, 0, 2); + $day = substr($bad_date, 2, 2); + $year = substr($bad_date, 4, 4); + return date($format, strtotime($month . '/01/' . $year)); + } + + // Date Like: MM-DD-YYYY __or__ M-D-YYYY (or anything in between) + if (preg_match('/^\d{1,2}-\d{1,2}-\d{4}$/',$bad_date)) + { + list($m, $d, $y) = explode('-', $bad_date); + return date($format, strtotime("{$y}-{$m}-{$d}")); + } + + // Any other kind of string, when converted into UNIX time, + // produces "0 seconds after epoc..." is probably bad... + // return "Invalid Date". + if (date('U', strtotime($bad_date)) == '0') + { + return "Invalid Date"; + } + + // It's probably a valid-ish date format already + return date($format, strtotime($bad_date)); + } +} + +// ------------------------------------------------------------------------ + +/** * Timezone Menu * * Generates a drop-down menu of timezones. |