$f=explode ($result,"-");
echo $f[0];  //2003
echo $f[1];   //4
echo $f[2];   //7

解决方案 »

  1.   

    如果有固定的分隔符,用楼上的方法;
    如果没有分隔符,知道所需数据的位置和长度,用substr();
    $thedate="2003-4-7";
    $year=substr($thedate,0,4);//$year='2003'
    $month=substr($thedate,5,1);//$month='4'
    $day=substr($thedate,7,1);//$day='7'
     
      

  2.   

    我使用了下面的方法:
    $thedate=  "2003-4-7  ";  
    $year=substr($thedate,0,4);//$year='2003'  
    $month=substr($thedate,5,1);//$month='4'  
    $day=substr($thedate,7,1);//$day='7'  
    第一种方法我很喜欢因为代码少但是我搞不成功以为取出来只有一个“-”
    我的数据库里字段是DATE类型有固定分隔符如“2003-04-07”
      

  3.   

    substr()函数的起始数从0算起,如你的日期格式为"2003-04-07";
    $thedate="2003-04-07";  
    $year=substr($thedate,0,4);//$year='2003',0-3位是年  
    $month=substr($thedate,5,2);//$month='04',5,6位是月  
    $day=substr($thedate,8,2);//$day='07',8,9位是日.
    如果月和日,有的存为1位,有的存为2位,则只能用explode()分隔.
    如果将一行文字分成不同子串,各子串之间又没有明显的分隔符,则只能用substr();
      

  4.   

    使用strtotime函数将其转换为时间类型,然后用date函数分别输出。
    strtotime函数可以识别所有合法的日期格式$thedate=  "2003-4-7  ";  
    $d = strtotime($thedate);
    echo date("Y",$d);
    echo date("m",$d);
    echo date("d",$d);