views:

36

answers:

2

Im receiving data with a created time field given in a unix timestamp format for example "1251324809" which to my understanding is the amount of time elapsed since the epoch, 1970.

Ive got a DATETIME formatted field in a MYSQL table, and im using this to return the UNIX time to a YY-MM-DD HH:MM:SS format:

date('Y-m-d H:i:s', $timestamp);

and then storing it in the database. The problem is some of my dates are being stored accurately and some of them are being stored as 1970-01-01 02:00:00, its only a small percentage of the data but i need to know why? Am i perhaps receiving the data incorrectly sometimes?

+1  A: 

Am i perhaps receiving the data incorrectly sometimes?

Probably. January 1st, 1970 points to $timestamp being zero or a value that can't be parsed as a time stamp.

Pekka
would it perhaps be more elegant if i used isset to test if there is a value and if not assign it the current time?
Baadier
@Baadier that entirely depends on your database - but I would rather insert `0000-00-00 00:00:00` into the database if the timestamp is zero
Pekka
id like to be able to work with the data that is pulled, and with the content being twitter powered i can assume that the data will at least be current even if the timestamp is errornous. So with the twitter API only allowing search for 7 days i can assume that the content cannot be older than 7 days and hence im relatively comfortable in assigning the current time in the case of an incorrect timestamp being received.
Baadier
@Baadier ah okay. In that case, that should work.
Pekka
Thanks for the help
Baadier
A: 

I find that dates in php and mysql never work properly. Particualrly as I am in a vastly different timezone than my server.

In mysql I avoid the problem by having the date fields stored as int(11)

I then insert the php calculated timestamp (usually via time()) into them.

Zurtri