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:

  • HOUR
  • DAY
  • WEEK
  • 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.


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