Jump to content

Define css-class to be used based on the time?


Luggruff

Recommended Posts

Been googling this for hours now and I can't find something that doesn't give a parse error.

 

I found this thread: http://www.phpfreaks.com/forums/index.php?topic=160855 ..and tried to work around the answer but with no luck.

 

All I really want to do is something like this:

 

<td class="

 

<?php

if ($time<20:00&>1:00) { echo "class1";}

else if($time>20:00&<2:00) { echo "class2";}?>

 

"></td>

 

I want the class to change to another one if the user see the table cell within a specific hour of the day.

Link to comment
Share on other sites

have you tired echoing out $time and seeing how its formatted/displayed?

 

Pretty much not at all (displayed).

 

And maybe I could be more specific as to what I've tried...

 

When using this code

 

<?php
if ($time<20:00&>1:00) { echo "class1";}
else if($time>20:00&<2:00) { echo "class2";}?>

 

I get the response "Parse error: syntax error, unexpected ':' in /hermes/....php on line 180"

 

So I tried changing it to

 

<?php
if ($hhmm<2000&>0100) { echo "class1";}
else if($hhmm>2000&<0200) { echo "class2";}?>

 

and I get the response "Parse error: syntax error, unexpected '<' in /hermes/....php on line 180" (also with $time, so i tried $hhmm instead wich made no diff.)

 

So I tried changing it to

 

<?php
if ($hhmm<=2000&=>0100) { echo "class1";}
else if($hhmm=>2000&<=0200) { echo "class2";}?>

 

and I get the response "Parse error: syntax error, unexpected T_AND_EQUAL in /hermes/.....php on line 180"

 

I don't know enough php to check what's wrong or not, since I don't know the language as of yet more than enough to be able to sometimes use snippets of code for specific functions.

Link to comment
Share on other sites

So I managed to print the time..

 

<?php
putenv("TZ=Europe/Stockholm");
$t=time();
echo(date("H i",$t));
?>

 

..still though, been trying for two hours to use this in my favour, but with no luck -_-

 

Keep getting errors like "unexpected T_AND_EQUAL" and "unexpected T_GREATER_OR_EQUAL". Guess I'm just too dumb..

Link to comment
Share on other sites

<?php
if ($time<20:00&>1:00) { echo "class1";}
else if($time>20:00&<2:00) { echo "class2";}?>

 

PHP does not recognize 20:00 as a value as you have it. Date/times can be represented as a timestamp (which is an integer that represents the number of seconds since Jan 1, 1970) or as a string, e.g. "20:00". But, string values cannot be compared using < or > like you want. You will need to implement some actual logic to do the check for you.

 

EDIT: Your current comparisons look to be off. The first one checks for times before 8PM or after 1AM. But, the second is checking for times after 8PM or before 2PM.

Link to comment
Share on other sites

Look at your syntax for the "greater than or equal to" operator.

 

http://us.php.net/manual/en/language.operators.comparison.php

 

Thanks for the link, but still, that's what I've looked at for hours. Either I'm unable to get it or I've looked at it wrong for too long to get it.

 

I've finally gotten something to work and it was less messy

 

<?php
date_default_timezone_set('Europe/Stockholm');
$hour = date('H:i');
if ($hour > "22:00" && $hour < "23:50")
{ echo "class1"; }
else
{ echo "class2"; }
?>

 

or a complete example (since you never find them when looking for them and someone might stumble upon this):

 

<td class="

<?php
date_default_timezone_set('Europe/Stockholm');
$hour = date('H:i');
if ($hour > "22:00" && $hour < "23:50")
{ echo "twobgcurrent"; }
else
{ echo "twobg"; }
?>

"></td>

 

 

<?php
if ($time<20:00&>1:00) { echo "class1";}
else if($time>20:00&<2:00) { echo "class2";}?>

 

PHP does not recognize 20:00 as a value as you have it. Date/times can be represented as a timestamp (which is an integer that represents the number of seconds since Jan 1, 1970) or as a string, e.g. "20:00". But, string values cannot be compared using < or > like you want. You will need to implement some actual logic to do the check for you.

 

EDIT: Your current comparisons look to be off. The first one checks for times before 8PM or after 1AM. But, the second is checking for times after 8PM or before 2PM.

 

I did know that about Jan 1, 1970 actually.. still, saying that I need to implement logic is really unneccessary, as I already said that's what I'm struggling with -> To find logic.

 

you mean 8PM or after 1AM, and the second one say 8PM or after 2AM, right? How do you figure it says 2PM?

Link to comment
Share on other sites

I've finally gotten something to work and it was less messy

 

<?php
date_default_timezone_set('Europe/Stockholm');
$hour = date('H:i');
if ($hour > "22:00" && $hour < "23:50")
{ echo "class1"; }
else
{ echo "class2"; }
?>

 

I don't think this is doing what you think it is. That comparison is going to be interpreted as a string, and you can't compare strings that way.

Link to comment
Share on other sites

I think you need to start here.

http://www.php.net/manual/en/language.types.intro.php

http://php.net/manual/en/language.operators.comparison.php

 

You're trying to do tricks on a bike before learning to ride it.

 

Is there a LOT there to learn and understand? Yes. No-one will tell you the learning your first programming language is an easy task.

Link to comment
Share on other sites

On another note: Looks like it's gonna be a long night. Just realized I need to make sure it's the same weekday as too (say "if it's between 22:00 and 23:00 and today is tuesday, then class = "class1").

 

Any idea on how I should do that? Tried changing

 

if ($hour > "22:00" && $hour < "23:50")

 

To

 

if ($hour > "22:00" && $hour < "23:50" && D == "Mon")

 

and

 

if ($hour > "22:00" && $hour < "23:50" && $D == "Mon")

 

but seems to have no effect..

 

I've finally gotten something to work and it was less messy

 

<?php
date_default_timezone_set('Europe/Stockholm');
$hour = date('H:i');
if ($hour > "22:00" && $hour < "23:50")
{ echo "class1"; }
else
{ echo "class2"; }
?>

 

I don't think this is doing what you think it is. That comparison is going to be interpreted as a string, and you can't compare strings that way.

 

Well, I have a table cell, see, and I want it to use a .css class that makes it all gold and shiny when the time is between 22:00 and 23:50, and when it's not in between those times, it will just be sad and gray. I tested it, and it worked. So I'm not really following you when you say that I can't do it, since I just did.

 

You might want to further test that theory.

 

$high = '23:00:00';
$low = '22:59:59';
var_dump($high > $low); // RETURNS TRUE
var_dump($high < $low); // RETURNS FALSE

 

So, you're really just saying that 23:00:00 is more than 22:59:59? Obviously it is.

 

I think you need to start here.

http://www.php.net/manual/en/language.types.intro.php

http://php.net/manual/en/language.operators.comparison.php

 

You're trying to do tricks on a bike before learning to ride it.

 

Is there a LOT there to learn and understand? Yes. No-one will tell you the learning your first programming language is an easy task.

 

Why would I need to learn it from the start and completely, if I'm just making some tiny functions to supplement 99.999% HTML code a year? Using it as little as I am, I will remember as much of it when I need it as I do now when I need it and never knew it, if I ever learn it all.

Link to comment
Share on other sites

You might want to further test that theory.

 

$high = '23:00:00';
$low = '22:59:59';
var_dump($high > $low); // RETURNS TRUE
var_dump($high < $low); // RETURNS FALSE

 

Hmm, neat.

 

It's still not fool proof though, because it doesn't account for days. 01:00:00 would be < 23:59:59, even though it could technically be after.

Link to comment
Share on other sites

You might want to further test that theory.

 

$high = '23:00:00';
$low = '22:59:59';
var_dump($high > $low); // RETURNS TRUE
var_dump($high < $low); // RETURNS FALSE

 

Hmm, neat.

 

It's still not fool proof though, because it doesn't account for days. 01:00:00 would be < 23:59:59, even though it could technically be after.

 

Well I've found a way to account for the day, at least for what I need it for (just personal use, lots of code but little traffic)

 

Solved!

 

<td class="
<?php
date_default_timezone_set('Europe/Stockholm');
$hour = date('H:i');
$day = date('D');
if ($hour > "22:00" && $hour < "23:50" && $day == "Mon")
{ echo "cell_is_gold"; }
else
{ echo "cell_is_not_gold"; }
?>
">

 

So, whenever it's Monday, and the time is between 22:00 and 23:50, that <td> cell is gonna get a nice little change of class.

 

And whatever you say about how wrong it is, it is not, because it actually works like a charm for just that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.