Show DIV at specific times of the day (useful for showing "open hours" on a website)

I would like to create a function on my site so that I can show when my physical store is open.

I need to switch DIV visibilty based on time.

For example: 07: 00-15: 59 = Show div 16: 00-06: 59 = Hide div

I have really good knowledge in html5 and less. I can do simple things in jquery and php. I thought like crazy, but I can't find anything.

If there is anyone who can just point me in the right direction, I would be very happy.

Thanks to the good people :)

+3


source to share


4 answers


Here's an example.

//gets the current time. 
var d = new Date();
if(d.getHours() >= 7 && d.getHours() <= 15 ){
    $(".open").show();
    $(".closed").hide();
}
else {  
     $(".closed").show();
    $(".open").hide();
}

      



https://jsfiddle.net/16mnrL3b/

+2


source


You can use the following to get the system time:



    var date = new Date();

    var time = date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
    $('div').hide()
    if(date.getHours()>7&&date.getHours()<16){
      $('div').show()
    }
    else 
    if(date.getHours()>=16&&date.getHours()<7){
      $('div').hide()
    }

      

+1


source


You can try this

if ( date('H:i') >= "07:00" && date('H:i') <= "16:00" ) {
   echo 'Time between 7 and 16';
}else{
   echo 'Time is runned out';
}

      

+1


source


You can compare the current time with the time you need to show a specific div using jquery like this:

$('#mydiv').hide();
$('#myclosediv').hide();

if(today.getHours() >= 7 && today.getHours() < 16){
 $('#myopendiv').show();
}   
else{
 $('#myclosediv').show();
}

      

If the current time is between 7:00 am and 4:00 pm, then a div with id 'myopendiv' will be shown. Another div.

+1


source







All Articles