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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Splitting String??

    Hey,

    I have a date in the format MM-DD-YYYY contained within the variable $edate.
    I want to split it into 3 variables $yyyy, $mm, $dd containing the year, month, and day respectively.

    how can i do this??

    Thanks

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    $str = explode("-", $date);

    list($dd, $mm, $yyyy) = $str;

  • #3
    New Coder
    Join Date
    Mar 2003
    Location
    Dublin, Ireland
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Seeing as you called your variable $edate change to:
    PHP Code:
    $str explode("-"$edate);

    list(
    $dd$mm$yyyy) = $str
    TopHostingDeals - The best place for the best deals!
    "It's nice to be important, but it's more important to be nice!"

  • #4
    New to the CF scene
    Join Date
    Oct 2003
    Location
    Mesa, AZ
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you can do it like this too

    PHP Code:
    $edate explode("-"$edate);
    $dd=edate[0];
    $mm=edate[1];
    $yyyy=edate[2]; 
    the other way works too i figured...hey i mite as welll tell you this way too

  • #5
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the list() method does basically what you did, but writing the list function is quicker.

    You can find out more about the list function at http://www.php.net/list

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts

    well if ur all gonna be pedantic.....

    u dont really even need the other variable

    PHP Code:
    list($mm,$dd,$yyyy) = explode('-',$edate); 
    will do just fine
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)

  • #7
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    very true.

    I generally define that as a variable as often i will need it later in the script i am writing, as an array maybe.

    but if you need it once, that that would work fine.


  •  

    Posting Permissions

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