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 3 of 3
  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Reorder a string

    I have a string that looks like this 20061402. basically the date but they year is first for searching purposes. anyway what i need to know is how i take that string and make it look like this "02-14-2006". can this be done or is there a better way to store the date inthe db to search that i can still make look better than 20060214

  • #2
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i'd usually store as a unix timestamp but I don't know your situation enough, here is a way to parse it:
    Code:
    $str = '20060214';
    print $str = preg_replace('/^(\d{4})(\d{2})(\d{2})$/', '\2-\3-\1', $str);

  • #3
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When storing dates in your db, make sure you're using a proper date column type. It will store the date as yours is stored (YYYY-MM-DD). When retrieving that date, your db will have a formatting function that will allow you to have your date display however you'd like it to, much like PHP's date() function works on unix timestamps. The function will vary depending on your db (in MySQL it's DATE_FORMAT()), for example.


  •  

    Posting Permissions

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