Solved

Convert SQL datetime to timestamp

Posted on 2004-09-06
6
8,767 Views
Last Modified: 2011-09-20
Hi,
Im having a big problem with yet another simple task :)
Trying to get MS SQL to convert its datetime type to a UNIX timestamp
How can this be done?  I know in MySQL there is the UNIX_TIMESTAMP function, but how does this relate in mssql?
The problem exists becuase I am trying to use PHP to convert the mssql datetime to a unix timestamp, and then back.
If I get a date from the database such as 2004-Sep-03 05:09:00 and use the php function strtotime() on that, it converts it, but then using strftime() converts it back and it ends up being somewhere around 2010!!
Help!
0
Comment
Question by:maunded
  • 4
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
imrancs earned 125 total points
ID: 11994636
UNIX_TIMESTAMP()

If called with no argument, returns a Unix timestamp (seconds since '1970-01-01 00:00:00' GMT) as an unsigned integer. If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00'


here is how can you do it in MSSQL

select datediff(second, '1970-01-01 00:00:00','2004-Sep-03 05:09:00')

Imran
0
 
LVL 1

Author Comment

by:maunded
ID: 11995027
Imran my man you are a genius
0
 
LVL 10

Expert Comment

by:imrancs
ID: 11995210
Glad if I could help!!!


Imran
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 1

Author Comment

by:maunded
ID: 11995315
Actually that hasnt quite solved my problem I just found out, when I convert the timestamp back in php, its offset by +10hrs (something to do with the fact my timezone is -10 I guess
0
 
LVL 10

Expert Comment

by:imrancs
ID: 11995336
yes you are right, this difference is becuase of timezone.

UNIX_TIMESTAMP() actually calculates according to GMT.


Imran
0
 
LVL 10

Expert Comment

by:imrancs
ID: 11995421
here is how you can get difference (in hours) from GMT


select datediff(hour,getdate(), getutcdate()) AS GMTDiff


Imran
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now