changing a variable based on time of day

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Craig Keightley

    changing a variable based on time of day

    I want to set a value of a variabel $var based on the time of day
    eg
    if time is between 10:00pm and 9:00 am
    set $var = "foo"

    if time is between 9:00 am and 7.00pm
    set $var = "bar"

    if time is between 7.00pm and 10.00pm
    set $var to "foobar"


    how do i do this with the timestamp function?
    many thanks

    craig


  • Ken Robinson

    #2
    Re: changing a variable based on time of day


    Craig Keightley wrote:[color=blue]
    > I want to set a value of a variabel $var based on the time of day
    > eg
    > if time is between 10:00pm and 9:00 am
    > set $var = "foo"
    >
    > if time is between 9:00 am and 7.00pm
    > set $var = "bar"
    >
    > if time is between 7.00pm and 10.00pm
    > set $var to "foobar"
    >[/color]

    Here's one way of doing it:
    <?
    $now_time = date('G',strtot ime('now'));
    echo $now_time."<br> \n";
    $var = 'foo';
    switch (TRUE) {
    case ($now_time >= 9 && $now_time <= 19):
    $var = 'bar';
    break;
    case ($now_time > 7 && $now_time <= 22):
    $var = 'foobar';
    break;
    }
    echo '$var:' . $var . "<br>\n";
    ?>

    Ken

    Comment

    • NC

      #3
      Re: changing a variable based on time of day

      Craig Keightley wrote:[color=blue]
      >
      > I want to set a value of a variabel $var based
      > on the time of day
      > eg
      > if time is between 10:00pm and 9:00 am
      > set $var = "foo"
      >
      > if time is between 9:00 am and 7.00pm
      > set $var = "bar"
      >
      > if time is between 7.00pm and 10.00pm
      > set $var to "foobar"
      >
      > how do i do this with the timestamp function?[/color]

      First of all, there is no timestamp() function in PHP.
      You need to use date() function:

      $hour = date('G');
      $var = "foo";
      if (($hour >= 9) and ($hour < 19)) {
      $var = "bar";
      }
      if (($hour >= 19) and ($hour < 22)) {
      $var = "foobar";
      }

      Cheers,
      NC

      Comment

      Working...