views:

867

answers:

2

I'm inserting a DateTime into MsSQL using the GetUTCDate() function provided by MsSQL.

I need to convert the time in C# to show it as the Unix / MySQL integer, so that it can be eventually manipulated with PHP.

I believe the Unix / PHP / MySQL ticks start at 1/1/1970, but I'm not sure how I would convert the equiv MsSql / C# time into this unix standard.

Any help would be appreciated.

A: 
DateTime dt = something; //Get from db

TimeSpan ts = dt - new DateTime(1,1,1970); // off the top of my head, check order of params

long ticks = ts.TotalTicks; // again off the top of my head, check property name
TJMonk15
So basically just subtract the 1970 from the MsSQL.. which starts @ 0/0/0000 ?
footose
This is the method I used, even though both methods would work.Thanks.
footose
+2  A: 

You can do this in MSSQL relatively easily. For the current date:

SELECT DATEDIFF(s, CONVERT(DATETIME, '1970-01-01'), GETUTCDATE())

returns the INT number of seconds since 1/1/1970, which is the Unix timestamp.

Cowan
I believe you also need to make '1970-01-01' GMT so if you are in NY that would have to be '1970-01-01 05:00:00.000'
SQLMenace
Great point SQLMenace
Cowan