SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    May 2001
    Location
    Lahore pakistan
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date Format in mysql

    i am taking date input from user in a form . User will enter date in mm-dd-yyyy but mysql supports yyyy-mm-dd . Now what is way to change input date to mysql standard format . Like to_date(format,date) function in oracle.

  2. #2
    Victory shall be mine tubedogg's Avatar
    Join Date
    Mar 2001
    Location
    Medina, OH
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well assuming you are running the form with PHP, sure. Just use the date function. (freddydoesphp will probably come along and give you an easier way but for now, here ya go. )
    First convert the users' input into a timestamp and then convert that into the MySQL format.
    So,
    Code:
    $userdateinput = str_replace("-",",",$userdateinput);
    $timestamp = date("U",mktime(0,0,1,$userdateinput));
    $mysqldate = date("Y-m-d",$timestamp);
    Then use $mysqldate to put into the db.
    Kevin

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No not at all in fact tubedogg that is a nice way of doing that(the str_replace, I hadn't seen it done that way), except unless you need both of those variables $timestamp and $mysqldate, if not you could simply combine them and save maybe an nth of a second. But thats just me being picky.


    $mysqldate = date("Y-m-d",mktime(0,0,1,$userdateinput));
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    Victory shall be mine tubedogg's Avatar
    Join Date
    Mar 2001
    Location
    Medina, OH
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe Thanks freddy Now that I got my ego boost for the night...
    Kevin


Bookmarks

Posting Permissions

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