ORDER BY Date Order

ASC = oldest first

DESC = most recent first

Note it’s “MINUTE”, “HOUR”, etc not “MINUTES” or “HOURS” etc

Convert DateTime to Date

CAST('$EventDateTime'AS DATE)

Specifying Date and DateTime from a string

It is recommended to use CAST()

CAST('$Year-$Month-01' AS DATE)
CAST('$Year-$Month-01T00:00:00' AS DATE)
CAST('$Year-$Month-01T00:00:00' AS DATETIME)

Last Day Of Month

(This produces a DATE result, not DATETIME)

LAST_DAY('2019-01-01T00:00:00')
//Will produce '2019-01-31'

Get SQL Server’s Current DateTime

  //Get the sqlserver current datetime
  $query = mysqli_query($dblink, "SELECT *, NOW() as datetimenow FROM tblMyTable WHERE Id = 1");
  $result = mysqli_fetch_array($query, MYSQLI_ASSOC);
  $DateTimeNow = $result['datetimenow'];

Same as Now() but giving just the date:

CURDATE()

How is DateTime stored by MySQL?

From the 10.9 Date and Time Data Type Representation:

     1 bit  sign           (1= non-negative, 0= negative)
    17 bits year*13+month  (year 0-9999, month 0-12)
     5 bits day            (0-31)
     5 bits hour           (0-23)
     6 bits minute         (0-59)
     6 bits second         (0-59)
    ---------------------------
    40 bits = 5 bytes
UK Electronic Designer
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.