Date turn into 1970 after formatting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Date turn into 1970 after formatting

Post 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]);
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Date turn into 1970 after formatting

Post 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.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Date turn into 1970 after formatting

Post 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?
Last edited by nitediver on Sat Jun 05, 2010 11:25 am, edited 1 time in total.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Date turn into 1970 after formatting

Post 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?
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Date turn into 1970 after formatting

Post 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".
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Date turn into 1970 after formatting

Post 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.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Date turn into 1970 after formatting

Post by nitediver »

Is it done?
So I should use strtotime()?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Date turn into 1970 after formatting

Post by Jonah Bron »

Yup. That's the code I posted above.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Date turn into 1970 after formatting

Post 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().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Date turn into 1970 after formatting

Post 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 :)
Post Reply