SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    911
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How to convert this string into array?

    Hi guys,

    I have this very basic string.
    Code:
    2000-12-20
    The first 4 digits are year
    The 2nd 2 digits are month
    The last 2 digits are day

    I know this is very basic.
    Can you help me convert this into array,

    array('year' => 2000,
    'month' => 12,
    'day' => 20);

    Thank you very much in advanced.
    I Love coding...
    Don't click this link.

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $date '2000-12-20';
    $array explode('-'$date); 
    Denny Schlesinger
    web services

  3. #3
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    911
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks dude.
    I Love coding...
    Don't click this link.

  4. #4
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    515
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by solidcodes View Post
    thanks dude.
    you asked for an associative array. If you really need it, use:
    PHP Code:
    $date '2000-12-20';
    $temp explode('-'$date);

    $array = array('year' => $temp[0], 
                   
    'month' => $temp[1],
                   
    'day' => $temp[2]); 
    Denny Schlesinger
    web services

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    sscanf() to the rescue!

    PHP Code:
    sscanf('2000-12-20''%d-%d-%d'$array['year'], $array['month'], $array['day']); 
    If you don't mind having a bunch of other keys/values in the array, then another option is:

    PHP Code:
    $array date_parse('2000-12-20'); 
    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    911
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys very nice answers.
    I Love coding...
    Don't click this link.


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
  •