Page 1 of 1

PHP Time Script

Posted: Fri Aug 28, 2009 4:53 pm
by Wolf_22
I'm trying to make a script that can produce a CSS class upon a certain time of the day. I found the script below and gave it a shot hoping that I could save some time, but what I found is that this script is very inaccurate as it is outputting "midnight" while it is in fact around 4PM.

Below is the script:

Code: Select all

function timeBG(){
    $hourOffset = 0;
    $hourOffset = ($hourOffset * 3600);
    $currentHour = date("G",time() + $hourOffset);
    if($currentHour < 4){$time="midnight";}
    else if($currentHour < 7){$time="dawn";}
    else if($currentHour < 11){$time="morning";}
    else if($currentHour < 13){$time="noon";}
    else if($currentHour < 16){$time="afternoon";}
    else if($currentHour < 19){$time="dusk";}
    else if($currentHour < 22){$time="evening";}
    else{$time="midnight";}
    echo $time;
}
I can do the leg work, but I'm not sure where to start exactly as I've never messed around with the PHP time functions, etc. Where would I start? Would it be with making a Unix time stamp or something or should I try to do something else to make it more accurate?

Re: PHP Time Script

Posted: Fri Aug 28, 2009 5:15 pm
by sousousou
Have you echo'ed the values of currenthour? For some reason it must be lower than 4, 22 or higher or anything else (like 0 or null or whatever).

Maybe try
$GMT = date("H");
$currentHour = $GMT + $hourOffset;

When that works start combining again? I don't know :p

Re: PHP Time Script

Posted: Fri Aug 28, 2009 6:22 pm
by mikemike
Try this:

Code: Select all

function timeBG(){
  
  $currentHour = date("G",time());
   if($currentHour < 4){$time="midnight";}
   else if($currentHour < 7){$time="dawn";}
   else if($currentHour < 11){$time="morning";}
   else if($currentHour < 13){$time="noon";}
   else if($currentHour < 16){$time="afternoon";}
   else if($currentHour < 19){$time="dusk";}
   else if($currentHour < 22){$time="evening";}
   else{$time="midnight";}
   echo $time;
 }
Not sure why it's adding an hour onto the offset. The above works fine for me, but you should be returning the value, not echoing it (if you want to be picky).

Re: PHP Time Script

Posted: Fri Aug 28, 2009 7:08 pm
by Wolf_22
mikemike wrote:Try this:

Code: Select all

function timeBG(){
  
  $currentHour = date("G",time());
   if($currentHour < 4){$time="midnight";}
   else if($currentHour < 7){$time="dawn";}
   else if($currentHour < 11){$time="morning";}
   else if($currentHour < 13){$time="noon";}
   else if($currentHour < 16){$time="afternoon";}
   else if($currentHour < 19){$time="dusk";}
   else if($currentHour < 22){$time="evening";}
   else{$time="midnight";}
   echo $time;
 }
Not sure why it's adding an hour onto the offset. The above works fine for me, but you should be returning the value, not echoing it (if you want to be picky).
I think the reason it's echoing the value is because it's a CSS class. So in the body tag, id would be as follows:
<body id="<?php timeBG();?>">

If you know of a better way to do this, I'm all eyes / ears. This was just something I got from a quick Google and thought that I could change it to whatever I have to have.

Re: PHP Time Script

Posted: Fri Aug 28, 2009 9:07 pm
by Wolf_22
mikemike, it works for me when I upload it to a live server, too.

I think it has something to do with this script being on my laptop's dev server.

Re: PHP Time Script

Posted: Sat Aug 29, 2009 12:22 am
by Wolf_22
morbid wrote:try to find it here.they got good resources.

http://www.phpscriptor.com
Got it bookmarked. :)