Date Time Code

  Home arrow Date Time Code arrow FormatMySQLDateTime
DATE TIME CODE

FormatMySQLDateTime
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2003-02-03

    Table of Contents:

     
     

    SEARCH CODEWALKERS

    Receives a mysql datetime format: date, time, datetime, timestamp, year. Returns formated date/time string based on PHP symbols .


    By : trube


    /**
    str FormatMySQLDateTime (mysql time, mysql datetime column type, string format)

    Receives a mysql datetime format: date, time, datetime, timestamp, year
    returns formated date/time string based on PHP symbols

    mysql date format: YYYY-MM-DD
    mysql time format: either HH:MM or HH:MM:SS
    mysql datetime format: YYYY-MM-DD HH:MM:SS
    mysql timestamp format: YYYYMMDDHHMMSS
    mysql year format: YYYY

    l (lowercase 'L') - day of the week, textual, long; e.g. "Friday"
    D - day of the week, textual, 3 letters; e.g. "Fri"
    F - month, textual, long; e.g. "January"
    M - month, textual, 3 letters; e.g. "Jan"
    n - month without leading zeros; i.e. "1" to "12"
    m - month; i.e. "01" to "12"
    j - day of the month without leading zeros; i.e. "1" to "31"
    d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"
    S - English ordinal suffix for the day of the month, 2 characters; i.e. "st", "nd", "rd" or "th"
    Y - year, 4 digits; e.g. "1999"
    y - year, 2 digits; e.g. "99"
    z - day of the year; i.e. "0" to "365"

    a - "am" or "pm"
    A - "AM" or "PM"
    g - hour, 12-hour format without leading zeros; i.e. "1" to "12"
    G - hour, 24-hour format without leading zeros; i.e. "0" to "23"
    h - hour, 12-hour format; i.e. "01" to "12"
    H - hour, 24-hour format; i.e. "00" to "23"
    i - minutes; i.e. "00" to "59"
    s - seconds; i.e. "00" to "59"

    $asc_datetime_format['long date'] = 'l, F j, Y';
    $asc_datetime_format['normal date'] = 'F j, Y';
    $asc_datetime_format['medium date'] = 'M j, Y';
    $asc_datetime_format['short date'] = 'M-d-y';
    $asc_datetime_format['numeric date'] = 'n/j/y';
    $asc_datetime_format['normal time'] = 'g:i a';
    $asc_datetime_format['time with seconds'] = 'g:i:s a';
    $asc_datetime_format['datetime'] = 'F j, Y g:i a';

    */
    function FormatMySQLDateTime($mysql_datetime_str, $mysql_datetime_type='date', $php_str_format='F j, Y') {
    if (!empty($mysql_datetime_str)) {

    // DEFAULTS
    $hour = 0;
    $minute = 0;
    $second = 0;
    $month = 1;
    $day = 1;
    $year = 2003;

    $mysql_datetime_type = strtolower($mysql_datetime_type);

    switch ($mysql_datetime_type) {
    case 'date':
    $date_components = explode('-', $mysql_datetime_str);
    if (count($date_components) == 3) {
    list($year, $month, $day) = $date_components;
    } else {
    return false;
    }
    break;
    case 'time':
    $time_components = explode(':', $mysql_datetime_str);
    if (count($time_components) == 3) {
    list($hour, $minute, $second) = $time_components;
    } elseif (count($time_components) == 2){
    list($hour, $minute) = $time_components;
    } else {
    return false;
    }
    break;
    case 'datetime':
    $datetime_components = explode(' ', $mysql_datetime_str);
    if (count($datetime_components) == 2) {
    list($date_str, $time_str) = $datetime_components;
    $date_components = explode('-', $date_str);
    if (count($date_components) == 3) {
    list($year, $month, $day) = $date_components;
    } else {
    return false;
    }
    $time_components = explode(':', $time_str);
    if (count($time_components) == 3) {
    list($hour, $minute, $second) = $time_components;
    } elseif (count($time_components) == 2){
    list($hour, $minute) = $time_components;
    } else {
    return false;
    }
    } else {
    return false;
    }
    break;
    case 'timestamp':
    if (strlen($mysql_datetime_str) == 12) {
    $year = substr($mysql_datetime_str, 0, 4);
    $month = substr($mysql_datetime_str, 4, 2);
    $day = substr($mysql_datetime_str, 6, 2);
    $hour = substr($mysql_datetime_str, 8, 2);
    $minute = substr($mysql_datetime_str, 10, 2);
    $second = substr($mysql_datetime_str, 12, 2);
    } else {
    return false;
    }
    break;
    case 'year':
    if (strlen($mysql_datetime_str) == 4) {
    $year = $mysql_datetime_str;
    if (!in_array($php_str_format, array('Y','y'))) {
    $php_str_format = 'Y';
    }
    } else {
    return false;
    }
    break;
    default:
    return false;
    }
    return date($php_str_format, mktime($hour, $minute, $second,$month,$day,$year));
    } else {
    return false;
    }
    } // end func
    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Date Time Code Articles
    More By Codewalkers

    blog comments powered by Disqus

    DATE TIME CODE ARTICLES

    - DaysInSpan.php
    - MySQLdateSpan.php
    - DateSpan.php
    - FutureDateFormEntry.php
    - Generate Time Option List for Select stateme...
    - Current Age Script (v2)
    - class for some mysql imitated time functions
    - Current Age Script, up to the last day
    - filemtime_remote
    - Page Generation Time Figure-Outer
    - convGMT v2
    - Benchmarker
    - Simple PHP Calendar
    - Display message according to hour of day
    - Display Date Function

    Developer Shed Affiliates

     



    © 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap