Dolfa
Dolfa

Reputation: 792

DB2 timestamp PHP DateTime

After upgrade to PHP 5.3 my application is returning DB2 columns with Timestamp type as "2010-12-15-10.23.22.716000". This is causing problem for PHP DateTime function , as it fails with

$time = new DateTime("2010-12-15-10.23.22.716000");

Failed to parse time string (2010-12-15-10.23.22.716000) at position 25 (0): Unexpected character

It seems its having a problem with too much accuracy in DB2 timestamp. Can I somehow force connection to change timestamp format with it is fetching data into?

I am connecting to database with db2_connect function like this:

$this->connection = db2_connect ( $config ['dsn'], $config ['username'], $config ['password'] );

Edit: I would like to use solution mentioned below, but its not working for me and on my system (AS400) it runs weird. I have: $timeRec = DateTime::createFromFormat('Y-m-d-h.i.s.u',$value);

$value = $timeRec->format('Y-m-d H:i:s');

$value = new DibiDateTime($value);

If I do var_dump($value) after ->format, correct string is returned, but if I try feed this string to DibiDateTime, that has:

public function __construct($time = 'now', DateTimeZone $timezone = NULL)
{    
    if ($timezone === NULL) {
        parent::__construct($time);
    } 
}   

then it fails with:

function format() on a non-object

Even when I checked before with var_dump($timeRec) is proper DateTime object.

Upvotes: 2

Views: 2340

Answers (2)

jez001
jez001

Reputation: 31

I was using PDO and the default timestamp format returned at PHP 8.1 and i5/OS 7.2 meant I had to use the following format (note the "H" for 24 hour)

$date = DateTime::createFromFormat('Y-m-d H:i:s.u', $table['TIMESTAMPCOL']);

Upvotes: 0

vascowhite
vascowhite

Reputation: 18440

I think your function (whichever it is) is choking on the microseconds at the end of your date string. Use the DateTime::createFromFormat() function to cope with this using the 'u' microsecond tag. See here http://php.net/manual/en/function.date.php

$time = DateTime::createFromFormat('Y-m-d-h.i.s.u', '2010-12-15-10.23.22.716000');
var_dump($time);

Output on my system:-

object(DateTime)[1]
  public 'date' => string '2010-12-15 10:23:22' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

Upvotes: 8

Related Questions