Solved

Convert SQL datetime to timestamp

Posted on 2004-09-06
6
8,779 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
point in time restore in SQL server 26 42
How can I find this data? 3 24
Return Rows as per Quantity of Columns Value In SQL 6 27
Regarding Disk IO 3 42
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

763 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