You have a date, time, or both and you need to convert it to Epoch seconds.

Use the DateDiff() function to do the conversion. If you are converting from local time, you'll first need to convert your date/time to GMT(Greenwich Mean Time) using DateConvert().

If you have ever worked on a *nix system before, or with other development languages such as Perl, Java, or even JavaScript, you may already be familiar with the concept of Epoch time. In the *nix world, the Epoch is defined as Jaunary 1, 1970 00:00 (midnight) GMT. This date/time is used as the starting point for all date and time calculations. By converting all dates following the Epoch to seconds, it makes it easy to do things like date comparisons, as well as adding to or subtracting from dates.

ColdFusion does not natively use the Epoch for date/time calculations. However, there may be various instances where you find you need to convert a date/time value to Epoch seconds for use in another environment.

To convert a ColdFusion date/time object to Epoch seconds, you can use the DateDiff() function to calculate the difference in seconds between the Epoch and the date/time you want to convert. If the date you want to convert to Epoch seconds is not in GMT (also called UTC or Universal Coordinated Time), you'll need to convert the Epoch to local time, or convert your local time to GMT. This is done using DateConvert().

<cfset thedate = createdatetime(2002,12,31,19,0,0)>
<cfoutput>
#TheDate# (local)<br>
Epoch seconds (convert Epoch to local time): #DateDiff("s",
  DateConvert("utc2Local", "January 1 1970 00:00"), TheDate)#<br>
Epoch seconds (convert local time to UTC): #DateDiff("s", "January 1 1970
  00:00", DateConvert("Local2utc", TheDate))#
</cfoutput>

Running this code produces this output:

{ts '2002-12-31 19:00:00'} (local)
Epoch seconds (convert Epoch to local time): 1041379200
Epoch seconds (convert local time to UTC): 1041379200

This question was written by Rob Brooks-Bilson
It was last updated on January 6, 2006.

Categories

Dates/Times

Comments

comments powered by Disqus