2

I have oracle database field with timestamp data-type.I want to convert this this value in cakephp controller into date format its not working properly it show 1969 year.

28-NOV-07 11.49.55.000000 AM this is coming after finding database and i want to convert it into 28.11.07 11:49:55.000

5
  • Have you tried date_parse?
    – Ultimater
    Commented Oct 15, 2012 at 11:41
  • Or just strtotime()? Commented Oct 15, 2012 at 11:46
  • yes i try this two functions but nothing works. Commented Oct 15, 2012 at 12:05
  • Is it possible to fetch timestamp from db in desired format with oracle db functions? Commented Oct 15, 2012 at 12:20
  • @SergeyP.akaazure, actualy I dont know how to get desired format in fetching time in cakephp find conditions. Commented Oct 15, 2012 at 13:02

2 Answers 2

2

You can try something like this:

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

After you get the date, just create a new one in your desired format.

I haven't tested it though, but something like that should work.

3
  • Thanks for your reply.I tested this code in my controller but it showing blank page.and no error occured.please help me. Commented Oct 15, 2012 at 12:07
  • 1
    @AvickMukherjee You should probably increase error_reporting in php.ini
    – KingCrunch
    Commented Oct 15, 2012 at 12:20
  • 1
    @DainisAbols I know this is an old post but the format identifier 'B' doesn't exists... It got me confused for a while. I think you meant 'A' or 'a' for the am and pm thing. php manual
    – Timber
    Commented Sep 8, 2014 at 18:49
0

I had to do this the long way. Means parsing the date with a regex and using mktime() to get a unix timestamp. In PHP it looks like this:

// 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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.