Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2003
    Posts
    61

    Unanswered: need help with mysql date and php

    i want people to be able to type in the date in normal form:
    for example:
    12/25/2003
    This needs to be converted to mysql 2003-12-25

    Here's my code:
    $date_input = $DATE;
    $date_output = preg_replace('-^(\d\d)/(\d\d)/(\d\d\d\d)$-', "\\3-\\1-\\2", $date_input);
    It works
    My problem is that the day has to be 2 digits, and so does the month. Therefore:

    2/2/2004 does not work. The user must type in 02/02/2004

    so i tried:

    $DATE = date("d/m/Y");

    $date_input = $DATE;
    $date_output = preg_replace('-^(\d\d)/(\d\d)/(\d\d\d\d)$-', "\\3-\\1-\\2", $date_input);

    That didn't work...
    Can anyone help?

    Thanks
    Noam

  2. #2
    Join Date
    Jan 2004
    Location
    Germany
    Posts
    17
    lol
    See your other topic, there is a function that does exactly that.

    http://www.php.net/strtotime

    PHP Code:
    $input_date "2/2/2004";

    $unix_timestamp strtotime($input_date);
    $mysql_compatible_date date("Y-m-d"$unix_timestamp);

    // of course you can do it in a single statement
    $mysql_compatible_date date("Y-m-d"strtotime($input_date)); 

Posting Permissions

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