Page 1 of 1

Timestamp to date error

PostPosted: Fri May 18, 2012 7:13 am
by Petrus
Hi to all, second question today.....
I have to convert a timestamp column to date (format d-m-Y H:i:s).

I used the delegate class and the script works but... instead to display 2012 it displays 2038!
This is the code:

Code: Select all
class tables_pagine {
    function ts__display(&$record){
        $ts = (int)$record->strval('ts');
        return date('d-m-Y H:i:s', $ts);
    }
}


This is what stored in the mysql timestamp column:

Code: Select all
2012-05-18 13:41:18


... And this is the result in the view:

Code: Select all
19-01-2038 04:14:07


I tried without forcing the cast and the result in the view is:

Code: Select all
10-07-1925 02:52:38


This is the environment:
PHP: 5.3.8
Mysql: 5.5.16

Hoping someone of you can help. :roll:

Re: Timestamp to date error

PostPosted: Fri May 18, 2012 7:52 am
by camt

Re: Timestamp to date error

PostPosted: Fri May 18, 2012 9:31 am
by shannah
Date fields are stored as arrays in Xataface. Use the strval() method instead of val() to make sure you're getting it in string form. Then use the strtotime function as camt suggests.

e.g.
Code: Select all
$ts = strtotime($record->strval('ts'));


-Steve

Re: Timestamp to date error

PostPosted: Fri May 18, 2012 9:44 am
by shannah
Whoops.. I see you were already using strval().