Page 1 of 1

Date turn into 1970 after formatting

Posted: Sat Jun 05, 2010 10:59 am
by nitediver
I have this weird problem when formatting timestamp date from mysql.

Current : 2010-05-06 19:49:33
After : 01 - 01 - 1970 07:33:30
This is my code:

Code: Select all

$datkonf = date("d - m - Y H:i:s", $rkonf[konf_submit]);

Re: Date turn into 1970 after formatting

Posted: Sat Jun 05, 2010 11:06 am
by Jonah Bron

Code: Select all

$datkonf  = date("d - m - Y H:i:s", strtotime($rkonf[konf_submit]));
Date() expects a timestamp, not a date string. Strtotime() makes a timestamp for you.

Re: Date turn into 1970 after formatting

Posted: Sat Jun 05, 2010 11:20 am
by nitediver
Jonah Bron wrote:

Code: Select all

$datkonf  = date("d - m - Y H:i:s", strtotime($rkonf[konf_submit]));
Date() expects a timestamp, not a date string. Strtotime() makes a timestamp for you.
But I store "$rkonf[konf_submit]", as timestamp format in mysql.
Is that doesn't matter, so that still act as string?

Re: Date turn into 1970 after formatting

Posted: Sat Jun 05, 2010 11:24 am
by Jonah Bron
I interpreted your post as stating that the date was stored in this format: 2010-05-06 19:49:33. You know that a Unix Timestamp is the number of seconds since Jan 1, 1970, right?

I might be confused. If you just echo the value of that column, what comes out?

Re: Date turn into 1970 after formatting

Posted: Sat Jun 05, 2010 11:33 am
by nitediver
Jonah Bron wrote:I interpreted your post as stating that the date was stored in this format: 2010-05-06 19:49:33. You know that a Unix Timestamp is the number of seconds since Jan 1, 1970, right?
Sorry I don't know.
I might be confused. If you just echo the value of that column, what comes out?
I got this "2010-05-06 19:49:33".

Re: Date turn into 1970 after formatting

Posted: Sat Jun 05, 2010 11:40 am
by Jonah Bron
Good. Strtotime() will take your string ("2010-05-06 19:49:33"), and return something like 1273200573. That is what date() wants.

Re: Date turn into 1970 after formatting

Posted: Sat Jun 05, 2010 11:46 am
by nitediver
Is it done?
So I should use strtotime()?

Re: Date turn into 1970 after formatting

Posted: Sat Jun 05, 2010 11:49 am
by Jonah Bron
Yup. That's the code I posted above.

Re: Date turn into 1970 after formatting

Posted: Sat Jun 05, 2010 11:51 am
by AbraCadaver
Yes, a MySQL timestamp is: YYYY-MM-DD hh:mm:ss. You can fetch it from the DB as a Unix timestamp with: http://dev.mysql.com/doc/refman/5.1/en/ ... -timestamp or use the strtotime().

Re: Date turn into 1970 after formatting

Posted: Sat Jun 05, 2010 11:55 am
by Jonah Bron
Cool. Didn't know you could get it as a Unix Timestamp right from the DB. I'm gonna have to read up on advanced SQL one of these days :)