{"id":2976,"date":"2020-07-01T16:52:34","date_gmt":"2020-07-01T15:52:34","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=2976"},"modified":"2024-04-10T15:18:25","modified_gmt":"2024-04-10T14:18:25","slug":"datetime-select","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/mysql\/queries\/select\/datetime-select","title":{"rendered":"DateTime SELECT"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Date Order<\/h4>\n\n\n\n<p>ASC = oldest first<br>DESC = most recent first<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Date or DateTime from strings<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM MyTableName\nWHERE (CreatedDateTime &gt;= CAST('2023-07-01' AS DATE)) AND\n  (CreatedDateTime &lt;= CAST('2023-07-31' AS DATE))\nORDER BY CreatedDateTime ASC \n\n\/\/Use AS DATETIME for DateTime fields<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Get time difference between now and a DateTime field<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  \"SELECT SomeField, TIMESTAMPDIFF(MINUTE, MyDateTimeField, now()) as MinsSinceMyDateTimeField FROM...\"\n  \/\/MinsSinceMyDateTimeField = now() - the DateTime field MyDateTimeField<\/code><\/pre>\n\n\n\n<p>Argument can be:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>MICROSECOND<\/li>\n\n\n\n<li>SECOND<\/li>\n\n\n\n<li>MINUTE<\/li>\n\n\n\n<li>HOUR<\/li>\n\n\n\n<li>DAY<\/li>\n\n\n\n<li>WEEK<\/li>\n\n\n\n<li>MONTH<\/li>\n\n\n\n<li>QUARTER<\/li>\n\n\n\n<li>YEAR<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">If DateTime value is Null?<\/h5>\n\n\n\n<p>You will get a Null value returned from TIMESTAMPDIFF<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  if ( (is_null($MinsSinceMyDateTimeField)) || ($MinsSinceMyDateTimeField &gt; 10) )\n  {\n  }<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Specifying Returned Date Format <\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>$sql = \"SELECT *,\n\t\tDATE_FORMAT(start_date, '%d\/%m\/%Y') AS date_start,\n\t\tDATE_FORMAT(end_date, '%d\/%m\/%Y') AS date_end\n\tFROM project_profile WHERE project_id = $project_id\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Get SQL Server\u2019s Current DateTime<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/\/Get the sqlserver current datetime\n  $sql = \"SELECT NOW() as datetimenow\";\n\n  $DateTimeNow = $Result&#91;'datetimenow'];<\/code><\/pre>\n\n\n\n<p>Same as Now() but giving just the date:<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Date Order ASC = oldest firstDESC = most recent first Date or DateTime from strings Get time difference between now and a DateTime field Argument can be: If DateTime value is Null? You will get a Null value returned from TIMESTAMPDIFF Specifying Returned Date Format Get SQL Server\u2019s Current DateTime Same as Now() but giving [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[257,64],"tags":[],"class_list":["post-2976","post","type-post","status-publish","format-standard","hentry","category-datetime","category-select"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2976","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=2976"}],"version-history":[{"count":13,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2976\/revisions"}],"predecessor-version":[{"id":4605,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/2976\/revisions\/4605"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=2976"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=2976"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=2976"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}