PHP Time Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

PHP Time Script

Post 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?
sousousou
Forum Commoner
Posts: 29
Joined: Fri Aug 28, 2009 1:10 pm

Re: PHP Time Script

Post 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
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: PHP Time Script

Post 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).
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: PHP Time Script

Post 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.
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: PHP Time Script

Post 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.
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: PHP Time Script

Post by Wolf_22 »

morbid wrote:try to find it here.they got good resources.

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