What is the preferred format to store date/times in a SQL Server database when PHP is your primary language?

前端 未结 3 1675
野趣味
野趣味 2021-02-05 22:25

I am planning a PHP application that needs to store date/times in an MSSQL database. (For the curious, it is a calendar application.) What is the preferred format to store this

3条回答
  •  北荒
    北荒 (楼主)
    2021-02-05 23:07

    I would store the dates in the MS-SQL format to assist in using the date manipulation functions in T-SQL to their fullest. It's easier to write and read

    SELECT * FROM Foo
    WHERE DateDiff(d,field1,now()) < 1
    

    Than to try and perform the equivalent operation by manipulating integers

    To convert a MsSQL date into a unix timestamp use dateDiff:

    SELECT DATEDIFF(s,'1970-01-01 00:00:00',fieldName) as fieldNameTS
    FROM TableName
    WHERE fieldName between '10/1/2008' and '10/31/2008'
    

    To Convert an Unix Timestamp into a MsSQL Date, you can either do it in PHP:

    $msSQLDate = date("Y-m-d H:i:s", $unixDate );
    

    or in MsSQL

    INSERT INTO TableName ( 
      fieldName
    ) VALUES (
      DATEADD(s,'1970-01-01 00:00:00', ? ) 
    ) 
    

    Where parameter one is int($unixDate)

提交回复
热议问题