在PHP中将时间戳转换为日期

时间:2012-10-15 11:34:02

标签: php oracle11g cakephp-1.3

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

28-NOV-07 11.49.55.000000 AM这是在查找数据库后我想将其转换为28.11.07 11:49:55.000

2 个答案:

答案 0 :(得分:2)

您可以尝试这样的事情:

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

获得日期后,只需创建所需格式的新日期即可。

我没有测试过,但是这样的事情应该有效。

答案 1 :(得分: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);