SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    convert string d/m/y to date

    Hi,I have problem about convert string format d/m/y to date for insert to database
    but i don't know how to convert it
    Thanks

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by lung View Post
    Hi,I have problem about convert string format d/m/y to date for insert to database
    but i don't know how to convert it
    Thanks
    US date format is m/d/y
    UK date format is d-m-y

    Notice the difference in separators? If you use those different separators, then functions like strtotime will automatically interpret them correctly for you.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how to do it?
    example string is "15/06/09"
    but when i convert is "31/12/1969"

    I don't know what's format of datetime for insert to database
    What's different if i insert "d/m/y" and "Y/m/d"

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by lung View Post
    how to do it?
    example string is "15/06/09"
    but when i convert is "31/12/1969"
    Convert the "UK" formatted string to a date, where dashes are used to tell PHP that it's in d-m-y format:

    Code php:
    $ukdate = "15-06-09";
    $time = strtotime($ukdate);
    $mysqldate = date('Y-m-d H:i:s', $time);

    Then $mysqldate will be in an appropriate format to enter in the database.
    Last edited by paul_wilkins; Sep 8, 2010 at 06:58.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Another alternative would be using mysqls function str to date.

    Code:
    STR_TO_DATE($date,'%d/%m/%Y');
    Just make sure that $date is the same format as %d/%m/%Y

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Sillysoft View Post
    Another alternative would be using mysqls function str to date.

    Code:
    STR_TO_DATE($date,'%d/%m/%Y');
    Just make sure that $date is the same format as %d/%m/%Y
    That's a nice idea. Make use of the database features, I like it.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •