i have the date in the format

20021102 = yyyymmdd

list ($theday, $daynumber, $months) = split (' [ ]', $Dat);

Dat is the list menu with the items with value of the date.

the way it is above it splits by a space. this worked but i changed my date format to the one above and it no longer works. how would i split the date into the 3 fields now as there are no longer spaces inbetween

i cant change the format of it now. so it must stay like above

yyyymmdd

thanks
mark

    The easiest way is substr()-splitting:

    function splitter($date)
    {
    $a = array();
    $a[] = substr($date, 0, 4);
    $a[] = substr($date, 4, 2);
    $a[] = substr($date, 6, 2);
    return $a;
    }
    
    list($year, $month, $day) = splitter($date);
    

      The_Igel's code is certainly easier to understand (which is a very important thing for code to be!), but his "splitter" function (if it checked the format of the string as well) could be carried out by

      list($year, $month, $day) = sscanf($date, '%4d%2d%2d');
      

      Although better code (for some value of "better" - in the sense of "safer", perhaps) that I've just noticed would be

      if(sscanf($date, '%4d%2d%2d', &$year, &$month, &$day)!=3)
      { // The match failed - $date misformatted
      }
      
        Write a Reply...