$bool = preg_replace("/([0-9]{4})-([0-9]{2})-([0-9]{2})\s([0-9]{2}):([0-9]{2}):([0-9]{2})/",'2006-08-01 12:05:14',$date);或:$bool = preg_replace("/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/",'2006-08-01 12:05:14',$date);

解决方案 »

  1.   

    说实话不知道,等其他人来解答
    我用
    $bool = preg_match("/([0-9]{4})-([0-9]{2})-([0-9]{2})\s([0-9]{2}):([0-9]{2}):([0-9]{2})/",'2006-08-01 12:05:13',$date);

    $bool = preg_match("/([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})/",'2006-08-01 12:05:13',$date);
    都可以,但是ereg只有“ ”可以
    preg系列速度要比ereg系列快,所以只用preg_match
      

  2.   

    <script language=javascript>
    String.prototype.isTime = function()
    {
      var r = this.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/); 
      if(r==null)return false; var d = new Date(r[1], r[3]-1,r[4],r[5],r[6],r[7]); 
      return (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);
    }
    alert("2002-1-31 12:34:56".isTime());
    alert("2001-2-29 12:54:56".isTime());
    alert("2002-1-41 12:00:00".isTime());
    </script>
      

  3.   

    前几天刚写的一个函式:
    //长时间,形如 (2003-12-05 13:04:06)
    function isDateTime($str)
    {
        //$matches = Array ( [0] => 2003-12-05 20:2:28 [1] => 2003 [2] => - [3] => 12 [4] => 05 [5] => 20 [6] => : [7] => 2 [8] => 28 )
        $s = preg_match('/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2})(:)?(\d{1,2})\6(\d{1,2})$/', $str, $matches);    if( empty($s) )return False;
        if( False === checkdate ($matches[3], $matches[4], $matches[1]) )return False; 
        if( $matches[5]>24 || $matches[7]>60 || $matches[8]>60 )return False;    return sprintf("%04d-%02d-%02d %02d:%02d:%02d", $matches[1], $matches[3], $matches[4], $matches[5], $matches[7], $matches[8]);
    }
    =======================================
    调用方式:isDateTime(时间串)
    返回FALSE说明格式/日期不正确
    否则返回格式化过的标准时间传
      

  4.   

    我是才开始用正则。。
    为什么用ereg不行呢???
    preg_match我还不会呢
    谁知道原因?