{"id":466,"date":"2016-05-16T17:07:57","date_gmt":"2016-05-16T22:07:57","guid":{"rendered":"http:\/\/www.marcblase.com\/blog\/?p=466"},"modified":"2016-06-03T10:38:47","modified_gmt":"2016-06-03T15:38:47","slug":"time-conditional-in-php","status":"publish","type":"post","link":"https:\/\/ma.rcbla.se\/blog\/2016\/05\/time-conditional-in-php\/","title":{"rendered":"Time conditional in PHP"},"content":{"rendered":"<p>Working on a WooCommerce project and there are products that are available daily until they sell out or until noon so they can be delivered. I use this function and pair it with the <code>$product->is_in_stock()<\/code> from the Woo WC_product class to provide conditional content in the template.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction wc_valid_time() {\r\n    \r\n    \/\/ Straight PHP using DateTime class\t\r\n    \/\/ date_default_timezone_set('America\/Chicago');\r\n    \/\/ $current_time = new DateTime(); \r\n    \/\/ $current_time-&gt;setTimezone(new DateTimeZone('America\/Chicago'));\r\n\r\n    \/\/ or, let WP do the lifting, as TimeZone is already set\r\n    $current_time  = current_time(&quot;H:i&quot;);\r\n\r\n    $start_time\t   = &quot;00:01&quot;; \/\/ 12:01 am\r\n    $cutoff_time   = &quot;12:00&quot;; \/\/ 12:00 pm\r\n\t\r\n    $right_now \t= DateTime::createFromFormat('H:i', $current_time);\r\n    $time2 \t\t= DateTime::createFromFormat('H:i', $start_time);\r\n    $time3 \t\t= DateTime::createFromFormat('H:i', $cutoff_time);\r\n\t\r\n\t\r\n\t\r\n\tif ($right_now &gt; $time2 &amp;&amp; $right_now &lt; $time3):\r\n\t\treturn true;\r\n\telse :\r\n\t\treturn false;\r\n\tendif;\r\n\t\r\n}\r\n<\/pre>\n<p><small>EDIT 6\/3\/16:<\/small><\/p>\n<p>It became necessary to filter for day of week as well as time of day, so below is how I did that:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nfunction wc_valid_day() {\r\n\t\r\n\t\/\/ timezone is already set in WP control panel\r\n\t\/\/ date_default_timezone_set('America\/Chicago');\r\n\t\r\n\t$current_day \t= current_time('D'); \/\/ day as three letter text\r\n\t\/\/ $current_time \t= new DateTime();\r\n\t\/\/ $current_time-&gt;setTimezone(new DateTimeZone('America\/Chicago'));\r\n\t$saturday\t\t= 'Sat';\r\n\t$sunday\t\t\t= 'Sun';\r\n\t\r\n\t$what_day \t\t= DateTime::createFromFormat('D', $current_day);\r\n\t$date2 \t\t\t= DateTime::createFromFormat('D', $saturday);\r\n\t$date3 \t\t\t= DateTime::createFromFormat('D', $sunday);\r\n\t\r\n\tif ($what_day == $date2 || $what_day == $date3):\r\n\t\treturn false;\r\n\telse :\r\n\t\treturn true;\r\n\tendif;\r\n}\r\n<\/pre>\n<p>Inside the loop on the template page for these products I use these two functions with the WooCommerce <code>is_in_stock()<\/code> function in a conditional to show the time\/day appropriate markup.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Working on a WooCommerce project and there are products that are available daily until they sell out or until noon so they can be delivered. I use this function and pair it with the $product->is_in_stock() from the Woo WC_product class to provide conditional content in the template. function wc_valid_time() { \/\/ Straight PHP using DateTime [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-466","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/comments?post=466"}],"version-history":[{"count":6,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/466\/revisions"}],"predecessor-version":[{"id":473,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/466\/revisions\/473"}],"wp:attachment":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/media?parent=466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/categories?post=466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/tags?post=466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}