Date or DateTime from strings

SELECT * FROM MyTableName
WHERE (CreatedDateTime >= CAST('2023-07-01' AS DATE)) AND
  (CreatedDateTime <= CAST('2023-07-31' AS DATE))
ORDER BY CreatedDateTime ASC 

//Use AS DATETIME for DateTime fields

Get time difference between now and a DateTime field

  "SELECT SomeField, TIMESTAMPDIFF(MINUTE, MyDateTimeField, now()) as MinsSinceMyDateTimeField FROM..."
  //MinsSinceMyDateTimeField = now() - the DateTime field MyDateTimeField

Argument can be:

  • MICROSECOND
  • SECOND
  • MINUTE
  • HOUR
  • DAY
  • WEEK
  • MONTH
  • QUARTER
  • YEAR
If DateTime value is Null?

You will get a Null value returned from TIMESTAMPDIFF

  if ( (is_null($MinsSinceMyDateTimeField)) || ($MinsSinceMyDateTimeField > 10) )
  {
  }

Specifying Returned Date Format

$sql = "SELECT *,
		DATE_FORMAT(start_date, '%d/%m/%Y') AS date_start,
		DATE_FORMAT(end_date, '%d/%m/%Y') AS date_end
	FROM project_profile WHERE project_id = $project_id";

Get SQL Server’s Current DateTime

  //Get the sqlserver current datetime
  $sql = "SELECT NOW() as datetimenow";

  $DateTimeNow = $Result['datetimenow'];

Same as Now() but giving just the date:

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *