php - Wordpress | Apply function at specific times of the day -
i have function hide woocommerce category based on category slug. see here:
class="snippet-code-html lang-html prettyprint-override">/* exclude category shop*/ add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 ); function get_subcategory_terms( $terms, $taxonomies, $args ) { $new_terms = array(); // if product category , on shop page if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) { foreach ( $terms $key => $term ) { if ( ! in_array( $term->slug, array( 'suviche' ) ) ) { $new_terms[] = $term; } } $terms = $new_terms; } homecoming $terms; }
and have other code apply rules if time within frame marked ( 9am - 5pm )
class="snippet-code-html lang-html prettyprint-override"><?php $hr = date("h"); //get hr in terms of double digits $min= date("i"); //get minutes in terms of double digits $t = ($hr*60)+$min; //convert current time minutes $f = (60*9); //calculate 9:00am in minutes $s = (60*17); //calculate 5:00pm in minutes if(($t>f || $t<s)) //if current time between 9:00am 5:00pm don't apply function { //do nothign } else //otherwise show execute function { //execute function } ?>
what want run filter hide product category if out of time fram ( 9am - 5pm )
any ideas great!
so far have nothing:
class="snippet-code-html lang-html prettyprint-override">/* exclude category shop*/ add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 ); function get_subcategory_terms( $terms, $taxonomies, $args ) { $new_terms = array(); $hr = date("h"); //get hr in terms of double digits $min= date("i"); //get minutes in terms of double digits $t = ($hr*60)+$min; //convert current time minutes $f = (60*9); //calculate 9:00am in minutes $s = (60*17); //calculate 5:00pm in minutes // if product category , on shop page if ( ( $t>f || $t<s) && in_array( 'product_cat', $taxonomies ) && ! is_admin() ) { foreach ( $terms $key => $term ) { if ( ! in_array( $term->slug, array( 'suviche' ) ) ) { $new_terms[] = $term; } } $terms = $new_terms; } homecoming $terms; }
thanks 1 time again whoever might help!
hi problem you're not getting 9am day. getting 9am 1970, jan 1st. also, wouldn't suggest working minutes. work seconds.
whenever work time in php, know beneficial utilize unix timestamp. unix timestamp running count of number of seconds have elapsed since 1st january, 1970.
a handy converter can found here
try instead.
$curtime = strtotime("now"); //get current time $finishtime = strtotime('9am '.date('d-m-y')); //calculate 9:00am in seconds $starttime = strtotime('5pm '.date('d-m-y')); //calculate 5:00pm in seconds if(!($curtime > $starttime && $curtime < $finishtime)){ //if here we're outside of time range. }
what we're doing here using strtotime("now")
current time, , date()
todays date, beingness concatenated time want(9am , 5am). we're using strtotime, convert whole thing unix timestamp. can compare later see if current time greater or lesser than.
also, these variables need $
in front end of them.
( $t>$f || $t<$s )
hope helps.
php wordpress
No comments:
Post a Comment