2

I want to show and hide div based on day and time. I have not much experience with PHP, but I think I found a code for this. But I don't know how to sort and how the codes shall be set up.

I want it to show like this during opening hours: http://boxmedia.no (bottom of frontpage).

It's a form with one field and one button. It's in Norwegian and says: Fill in your number here. And press here, then we call you back in 10 mins

This is the code for the form:

<div class="container"> <div class="row-centered main-btn ring_me"> 
<?php 
echo do_shortcode('[contact-form-7 id="249" title="Untitled"]'); 

?> </div> <!--div class="row-centered sec-btn" style="display:none;">
<span><input max-size="30" type="text" id="contact_no" name="contact_no"/>
</span><span class="btn btn-success btn-lg">
<font><font class="goog-text-highlight">
<a href="<?php echo home_url(); 

?>/kontakt-oss">Send </a> </font></font> </span></div--> </div>

And this is the code I think will make it show during opening hours: 8am to 4pm. And not in weekends.

But where do I put this in the existing code above:

<?php date_default_timezone_set('Europe/Oslo');
$currentHour = date("H");
$openTime = 8;
$closeTime = 16;
if ($currentHour >= $openTime && $currentHour < $closeTime){
      $css = 'display:block;';
}else{
      $css = 'display:none;';
}

echo '<style type="text/css">.timeBasedLink {'.$css.'}</style>'; ?>


if (date("w") == 0 || date("w") == 6 || $currentHour < $openTime || 
$currentHour >= $closeTime)

{
    $css = 'display:none;';
}
else
{
    $css = 'display:block;';
}

--------------- Added February 2. 2015 ---------------

And is this the code to add to my css file? And is it placed correctly? Is the class .TimeOn ? How I can connect the TimeOn to ring_me or what the class is called?

.ring_me input[type="tel"] {
    width: 258px;
    margin-right: 20px;
.timeOn {
display:block;
}
.timeOff {
display:none;
}
}

And this is another place in the CSS:

.ring_me input[type="tel"] {
.timeOn {
display:block;
}
.timeOff {
display:none;
}
font-size: 20px;
line-height: 24px;
  • Sidenote: You have a curly quote `‘` in `‘Europe/Oslo'` - that will break your code. Change it to `'Europe/Oslo'` – Funk Forty Niner Jan 30 '15 at 13:06
  • 2
    There is an attempted edit wanting to "fix" your code. Changing curly quote as mentioned above to a regular quote. I refused the edit. – Funk Forty Niner Jan 30 '15 at 13:08
  • @wayzz - Please do not "fix" code, as per your suggested edit. Fix spelling and indents, or tags. I rejected the edit. – Funk Forty Niner Jan 30 '15 at 13:10
  • You're welcome Tommy. Is the curly quote in fact part of your working code? It was in your original post. – Funk Forty Niner Jan 30 '15 at 13:10
  • Tommy, the edit was approved and I don't know why it was. If your code contains the curly quote, then replace it. If you approve of the edit, you can leave your question as is. If not, you can do a rollback. Editors should not "fix" code in questions. – Funk Forty Niner Jan 30 '15 at 13:13
  • Sorry @Fred-ii- , this is my first question in here. And im a PHP-beginner. I think the curly qoute is wrong as you said. I fixed it so it is like the original. – Tommy van Wallinga Jan 30 '15 at 13:16
  • It's not your fault Tommy, we've pretty much all been there at one point. If you did change your actual code to have regular quotes like `'Europe/Oslo'` then you can edit your code/question again to reflect the change to avoid confusion. Cheers – Funk Forty Niner Jan 30 '15 at 13:18
  • 1
    Yes I edited it so the quotes is correct like this: 'Europe/Oslo' – Tommy van Wallinga Jan 30 '15 at 13:30

4 Answers4

4

Okay. You have some problems with your code, let's start there. First:

date_default_timezone_set(‘Europe/Oslo');

These are the wrong quotes, it needs to be this:

date_default_timezone_set('Europe/Oslo');

PHP will only recognize single quote (') and double quote ("") for processing these kinds of things.

Second, when is your store open? You set the close time here:

$closeTime = 9;

To 9. But you're using $currentHour = date("H"); which is 24 hour code. That means your store will only be treated as open between 8 AM and 9 AM. Perhaps you meant 9 PM, which would be:

$closeTime = 21;

Third, Right here:

echo '<style type="text/css">.timeBasedLink {'.$css.'}</style>'; ?>

You prematurely end the PHP tag with ?>. This stops all the following PHP code from processing.

Fourth, You're also generating in-line CSS, which is bad, depending on if the store is open or closed. You can simply set up two CSS classes (outside the PHP code, none the less) and save yourself that additional processing, like so:

<style type="text/css">
    .timeOn {
        display:block;
    }
    .timeOff {
        display:none;
   }
</style>

Note that this is in-line CSS, and should really go in a normal CSS file.

Finally, to fix your actual problem, the send button is currently wrapped in comment tags:

<!-- // Comment Goes here -->

We're going to git rid of these tags, and simply echo our new PHP variable (as a class) inside the DIV to hide it or show it, like so:

<div class="row-centered sec-btn <?php echo $css; ?>">

This would be my implementation of the above code:

<?php 
    // Basic Variable Declaration
    date_default_timezone_set('Europe/Oslo');
    $currentHour = date("H");
    $openTime = 8;

    // This needs to be 21, if using "H" and the time is 9
    $closeTime = 21;

    // Here we check to see if we are open, and in a good week
    if ($currentHour >= $openTime && $currentHour < $closeTime && (date("w") != 0 || date("w") != 6)){
          // If we are, set our class to off.
          $css = 'timeOn';
    } else {
          // Otherwise, set our class to off.
          $css = 'timeOff;';
    }

    // This code should really go in a CSS file
    // And be inclunded in the header
    echo '<style type="text/css">';
        // This is our class for showing the block
        echo '.timeOn {
                display:block;
              }';
        // This is our class for hiding the block
        echo '.timeOff {
                display:none;
               }';      
        echo '</style>';

    // We're ending the PHP Tag here
?>

<div class="container"> 
    <div class="row-centered main-btn ring_me <?php echo $css; ?>"> 
        <?php 
            echo do_shortcode('[contact-form-7 id="249" title="Untitled"]'); 
        ?> 
    </div>
</div>

Note that you also had an extra set of <font> tags inside the final DIV that weren't doing anything.

This solution is not 100% correct (I left the CSS Generation in PHP so I could add comments to it, for example) but it uses defined classes and simply alters the class based on the PHP Hour variables we set up at the beginning of the script. This is a much cleaner approach that requires much less processing on the PHP side before generating the page.

Mark
  • 861
  • 9
  • 17
  • Thanks a lot Mark. So if I add this instead of the current code in home.php it will be correct? And what do I add in the css-file? This code: @Mark ? – Tommy van Wallinga Jan 30 '15 at 13:41
  • @TommyvanWallinga You only need to add the two classes into the CSS file, not the style tags.I believe this code will work, I am slightly unclear on exactly which DIV's you want to show/hide, but once you've added the PHP code to the file, it's just a matter of moving this block: into the class="" part of the DIV you want to show or hide. – Mark Jan 30 '15 at 13:56
  • 1
    @TommyvanWallinga, Mark's code is cleaner than what you originally posted, and is worth using. Please take note of his comment about the CSS being included in a CSS file. – SRing Jan 30 '15 at 14:00
  • Hi @Mark thanks a lot. But how do I put the code in CSS? and can I put it anywhere? I also found out that this code was not in use, so it could just be deleted: Send – Tommy van Wallinga Jan 30 '15 at 14:01
  • @TommyvanWallinga Yes, the code can go anywhere in the CSS file. I've updated my answer to include your note about the unused code. – Mark Jan 30 '15 at 14:28
  • I edited my question @Mark - Do you know how I can connect the TimeOn to ring_me or what the class is called? – Tommy van Wallinga Feb 02 '15 at 14:29
0

You could add it to your header.php below the stylesheets. Looking at the echo section you'll need to change the class used "timeBasedLink" to the class your form ouput.

echo '<style type="text/css">.timeBasedLink {'.$css.'}</style>'; ?>
Aaron
  • 10,187
  • 3
  • 23
  • 39
0

You should consider making it a function for convenience

<?php
function hide_show_from_date()
{
  date_default_timezone_set('Europe/Oslo');
  $currentHour = date("H");
  $openTime = 8;
  $closeTime = 9;
  $css = 'block';
  if ($currentHour >= $openTime && $currentHour < $closeTime)
  {
      $css = 'block';
  }
  else
  {
      $css = 'none';
  }
  if (date("w") == 0 || date("w") == 6 || $currentHour < $openTime || $currentHour >= $closeTime) {
      $css = 'display:none;';
  }
  else {
      $css = 'display:block;';
  }
  return $css;
}
?>

<div style="display:<?php hide_show_from_date();?>">
Your content here
</div>
Jaay
  • 2,103
  • 1
  • 16
  • 34
  • I tried. And it did show, but did not hide correctly @Jaay – Tommy van Wallinga Jan 30 '15 at 13:25
  • Can you be more constructive, like what is your error ? – Jaay Jan 30 '15 at 13:26
  • I tried to write open time and closetime from 8-9 and here it is 2pm, and it still is there. So it was no error. Only that it did not hide the element that i put in "Your content here" @Jaay – Tommy van Wallinga Jan 30 '15 at 13:32
  • I have updated the code you may try it it should work, also there is a problem in your code as you are comparing string from `date` function and integer in `$openTime` and `$closeTime`, the easiest way here is to change everything to string, so `$closeTime = 8` => `$closeTime = '8'` (same thing with openTime) – Jaay Jan 30 '15 at 13:46
0

A bit of theory first: PHP runs before the page loads and before the HTML is written. This means that you cannot simply "include" PHP in a HTML page. You have to include the HTML in a PHP page.

To do this, simply create a page with the .php extension (instead of .html), add your HTML, and then put the PHP code inside the proper tags.

<?php
  //Please see Mark's answer for a good example of the code.
?>

This does not differ in any other way from a regular html page. You'll have the normal structure of an HTML page and then simply add your PHP code between the <?php ?> tags. Like this:

<html>
<head>
  //This is where you link an external .css sheet
  <link href="Path-to-file/FILENAME.css" rel="stylesheet" type="text/css">
</head>
<body>

  <?php
    //Some PHP code
  ?>

  <!--Form Code goes here-->
  <form></form>

</body>
</html>

Essentially you are still writing an HTML page, but instead of manually writing it all out, you are using PHP to write different HTML depending on the time/day of the week.

See http://www.w3schools.com/css/css_howto.asp for information on CSS.

This can also be done using Javascript + PHP. First call a separate PHP page using AJAX, returning TRUE if it is within the correct time and displaying your form (see https://stackoverflow.com/a/7165616/4126804).

Community
  • 1
  • 1
SRing
  • 694
  • 6
  • 16