2

我有timestamp数据类型的 oracle 数据库字段。我想将 cakephp 控制器中的这个值转换为日期格式,它不能正常工作,它显示 1969 年。

28-NOV-07 11.49.55.000000 AM这是在找到数据库之后出现的,我想将其转换为28.11.07 11:49:55.000

4

2 回答 2

2

你可以尝试这样的事情:

$timestamp = "28-NOV-07 11.49.55.000000 AM";
$date = DateTime::createFromFormat( 'd-M-y h.i.s.u A', $timestamp );

获得日期后,只需以所需格式创建一个新日期。

虽然我还没有测试过,但类似的东西应该可以工作。

于 2012-10-15T11:44:31.703 回答
0

我不得不这样做很长的路要走。表示使用正则表达式解析日期并使用 mktime() 获取 unix 时间戳。在 PHP 中它看起来像这样:

// our date in oracle-style 
$orcl_date = "22-MAY-13 12.00.00.000000 AM";

// painfully extract date related parts
preg_match( '/^(\d{2})-(\w{3})-(\d{2})\s(\d{2})\.(\d{2})\.(\d{2})\.(\d{6})\s(AM|PM)/' , $orcl_date , $matches );
@list( $all , $day , $month , $year , $hour , $minute , $sec , $minisec , $ampm ) = $matches;

// do AM/PM illogical behaviour
if ( $ampm == 'AM' ) { // 12 AM (midnight) will result in zero.
    if ( $hour == 12 )
        $hour -=  12;
} else { 
    if ( $hour != 12 ) // 12 pm (high noon) 
        $hour +=  12;
}

$month = array_search( $month , array("","JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEZ" ) );

// oracle does not give us any information about the century. We assume that it's the current one.
$year += 2000;

// make a unix timestamp.
$timestamp = mktime( $hour , $minute , $sec , $month , $day , $year );

$result = strftime('%Y-%m-%d %H:%M:%S',$timestamp);
于 2013-03-28T16:14:04.677 回答