Forum Moderators: coopster
So, the MySQL TIMESTAMP(14) column is different to the PHP date/time functions?
How does one work with these values in PHP, for example: If you have a 14 digit MySQL TIMESTAMP field and want to find out how many hours have passed since the time now?
I guess there is a way of working with these values easily right?
Many thanks
Nick
PHP uses unix time to work out dates.
MySQL uses an ISO standard.
What I've just done when working with times is:
SELECT the field with
UNIX_TIMESTAMP(TimeStampField) then use the standard php functions to manipulate time as you want it.
E.g. to output the date & time read from the database you would use:
print date("d/M/y H:i",$datefromdatabase); Hope that points you on the right path!
JP
I suggest having a look here :
[mysql.com...]
The functions DATE_FORMAT, TIME_FORMAT, NOW, UNIX_TIMESTAMP, FROM_UNIXTIME are especially useful.
> How does one work with these values in PHP, for example: If you have a 14 digit MySQL TIMESTAMP field and want to find out how many hours have passed since the time now?
The timestamps are given in seconds since q reference date. Substract them, divide by 3600 to get the number of hours then truncate that value to the lower integer.
SELECT FLOOR((NOW() - my_timestamp)/3600) FROM my_table WHERE ...
The FLOOR function is here : [mysql.com...]
Jocelyn
The decider is normally how you are going to query the database - if your query includes calculations based on the time value then use DATE/TIME.
However if you're SELECTing based on some other criteria and are just going to process the date/time in PHP then simply store the INT.