|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
Reading Unix Timestamps
I'm a PHP veteran, but recently took up ColdFusion, so I'm sort of new to it. What I need to do is convert a Unix timestamp into a human-readable form. I know in PHP it's date("<format>", $timestamp), but can ColdFusion format a timestamp?
I searched the forums already, but found nothing. If I missed a thread that already discusses this, my apologies. |
|
#2
|
|||
|
|||
|
Check out the CF documentation, specifically the function reference. Will something like dateFormat() or timeFormat() work in this case?
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian. How to Post a Question in the Forums |
|
#3
|
||||
|
||||
|
Yeah, the CF documentation is my best friend and there's no simple function like in PHP (that I'm aware of). However, after several hours of playing, I have created a function that works. I have a habit of doing things the hard way, so if there's a better way, let me know:
<cfscript> function ConvertUnixTimestamp(input) { return DateConvert("utc2Local", Replace(Replace(DateAdd("s", input, "January 1 1970 00:00:00"), "{ts '", ""), "'}", "")); } </cfscript> |
|
#4
|
|||
|
|||
|
What does a UNIX timestamp look like? It seems bizarre to me that none of the built-in date functions will format it?
|
|
#5
|
|||
|
|||
|
|
|
#6
|
||||
|
||||
|
A Unix timestamp is the number of seconds that have elapsed since the Unix epoch (midnight of January 1st, 1970). It usually looks like this: 1070152114.
The UDF I wrote is the same thing, expect it strips off the preceeding "{ts '" and ending "'}" and converts the time to local time (as the timestamp is always in UTC). I suppose the UDF I wrote is the answer to my question. Thanx for the help! Last edited by MatthewClark : August 25th, 2004 at 09:33 AM. |
|
#7
|
||||
|
||||
|
Matthew, I tried your conversion function and it worked great, I just had to throw a dateFormat() around it's output to make it look how I wanted. However, I just made up a UNIX timestamp to try it with because I'm in a non-Unix environment.
I'm going to save this function in case I ever get back to Unix-land and am using CF. wdn2k |
|
#8
|
||||
|
||||
|
Quote:
Glad you can use it! Last edited by MatthewClark : August 25th, 2004 at 10:15 AM. |
|
#9
|
||||
|
||||
|
you could also just ask mysql to return the converted format
makes your CF code a lot cleaner the more you do in the database, the better ![]() |
|
#10
|
||||
|
||||
|
Quote:
I agree that having the DB take some of the load is a good idea, but sometimes I want the date as an int because it makes date math very simple. wdn2k |
|
#11
|
||||
|
||||
|
I agree, and I tried using CAST(), which worked. But the UDF I wrote makes the returned string a little cleaner and easier to use, plus, hopefully someone can use it...
|
|
#13
|
||||
|
||||
|
Quote:
but you can see it either way: Code:
select myunixtimecolumn as date_unix
, from_unixtime(
myunixtimecolumn
, '%Y-%m-%d %h:%i'
) as date_formatted
from mytable
of course, if you're on some other database instead of mysql, and it doesn't have a from_unixtime function, then... Last edited by r937 : August 25th, 2004 at 01:10 PM. |
|
#14
|
||||
|