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.
From stackoverflow
-
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
footose : 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. -
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.
SQLMenace : 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'Cowan : Great point SQLMenace
0 comments:
Post a Comment