Results 1 to 3 of 3

Thread: [RESOLVED] Convert Date

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Resolved [RESOLVED] Convert Date

    I ams torign a date in a mysql db it is inputted as DD/MM/YYYY and i need to transform it to YYYY/MM/DD then also swith it back to DD/MM/YYYY

    Anyone got a fucntion that could help me ?

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Convert Date

    preg_match()

    Here is a function which I use to convert a date in the format of yyyy-mm-dd to a UNIX timestamp. Obviously, you'll need to modify this to suit your needs; it will be benificial to you if you look at the Pattern Syntax for PCRE too.
    PHP Code:
    /**
     * Converts a date from FoxPro format to a UNIX time stamp.
     *
     * Converts a date in the format yyyy-mm-dd to a UNIX time stamp which can be used
     * with PHP's date/time functions.
     *
     * @author Adam Delves (adam @ sccode . com)
     * @copyright Copyright © all rights reserved Adam Delves
     *
     * @param string $date A date in the format yyyy-mm-dd
     * @return mixed null for a null date or a timestamp for a valid date as returned by mktime()
     */
    function format_date($date)
    {
        
    /* REGEXP: matches a stirng in the format dddd-dd-dd where 
                   d is any decimal number 0-9
         */
        
    $regex "/^(\d{4})\-(\d{2})\-(\d{2})$/";

        if (
    $date == '1899-12-30' || $date == '' ) { // empty/null date
        
    return null;
        } else if (
    preg_match($regex$date$matches)) {
            return 
    mktime(0,0,0$matches[2], $matches[3], $matches[1]);
        } else {
        throw new 
    Exception('Invalid Date Format'); // PHP 5 only
        
    }

    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: Convert Date

    Thanks for the help once again adam.

    I have done this using the SPLIT() function and its workign great now

    Thnaks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width