Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Date problem...

  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Date problem...

    Hello,

    I'm looking to post a (job)Date in a MYSQL database in the format of YYYY-MM-DD. However, when the user enters the date in the textbox I want it in the following format D-M-YYYY (IE 1-1-2008)

    So here's my code for posting the data for he database:

    PHP Code:
      if(!isset($_GET['note_id']))
      { 
      
    $result mysql_query("Insert into notes(jobDate,about) values('$jobDate','$about')");
      
    $note_id=mysql_insert_id();
      
    header("Location: edit_list.php"); 
      }
      else
      {
      
    $result mysql_query("Update notes set jobDate='$jobDate', about='$about' where note_id=".$_GET['note_id']);
      
    header("Location: edit_list.php"); 
       } 
    So how do I turn it around so that when I post the date in the database it coverts to a YYYY-MM-DD format, and when I get the data out of the database it is displayed in by textbox as D-M-YYYY for editing purposes.

    Any help most welcome ;-)

    Thank you

  • #2
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Hi stfc,

    Assuming your display date is in the precise format "D-M-YYYY" (and assuming that by "D" and "M" you are implying variable width format), you can use the following conversions:

    D-M-YYYY to YYYY-MM-DD:
    PHP Code:
    $tokens explode("-"$dmyyyy);
    $yyyymmdd $tokens[2]."-".sprintf($tokens[1], "%02d")."-".sprintf($tokens[0], "%02d"); 
    YYYY-MM-DD to D-M-YYYY:
    PHP Code:
    $tokens explode("-"$yyyymmdd);
    $dmyyyy intval($tokens[2])."-".intval($tokens[1])."-".$tokens[0]; 

  • Users who have thanked mlse for this post:

    stfc_boy (11-14-2007)

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Or you could do without the complicated conversion, and just use STR_TO_DATE.

  • #4
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Yes, that's clearly more sensible! I wasn't aware of the STR_TO_DATE function ...


  •  

    Posting Permissions

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