1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Change variable based on time and day?

Discussion in 'PHP' started by medialab, Apr 10, 2015.

  1. #1
    Hey Everyone,

    I am trying to write some PHP code to change a variable based on open hours

    M-S from 12:01 am until 6:30 pm
    Sun from 12:01 am until 4:30 pm

    I have started on the code below but I am sure there is an easier way of doing this, any help would be greatly appreciated!

    <?php
    
    $h = date('G'); //set variable $h to the hour of the day
    $d = date("w", $timestamp); //set variable $d to day of week
    
    if ($h > 8 && $h < 18 && ($d == 1 || $d == 2 || $d == 3 || $d == 4 || $d == 5)) $link = 'link1';
    else $link = 'link2';
    
    ?>
    PHP:
     
    medialab, Apr 10, 2015 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    
    $time = date('H:i');
    $d = date('w');
    
    $link = ($time >= '12:01' && $time <= '18:30') && ($d >= 1 && $d < 7) ? 'link1' : 'link2'; // this will check for time between 12.01 and 18.30, and monday to saturday - modify as needed
    
    PHP:
     
    PoPSiCLe, Apr 10, 2015 IP
  3. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #3
    What happens if the user is outside of those 2 time frames? Would there be a third link, or is it automatically in the first time frame regardless?
     
    ThePHPMaster, Apr 10, 2015 IP
  4. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #4
    Why would you base this off from server time?
     
    NetStar, Apr 11, 2015 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    If the server is in the same time-zone as the users accessing the site, this isn't necessarily a problem - that depends on what type of business is being done. And on how many possible time-zones you're dealing with (for instance) - for me, based in Norway, as long as the server runs on standard time here, I wouldn't have any problem doing this based on server-time, really. There's only one time-zone to care about. Of course, if you're in the middle of Europe, or in the US, and do business all over, then it's a different issue, and you'd be wise to pull the time from the user instead.
     
    PoPSiCLe, Apr 11, 2015 IP
  6. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #6
    Of course if the server is in the same time zone as ALL of the visitors there's nothing to worry about...................however...in the REAL WORLD we don't control where our visitors come from. Duh.

    But that is completely irrelevant...because the original poster is creating a script to control opening hours of a particular store or perhaps his web site. So the time zone would need to be based off the company's location which may be in a different time zone than the server. Even if by coincidence the server shares the same time zone we cannot rely on the time of the server. This could change do to a system admin change or if he completely moves his web site from one server to another.
     
    NetStar, Apr 11, 2015 IP
  7. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #7
    And again... not every part of the world have different time zones to consider - if you have a local website, in your local language, you won't be very concerned if someone from across the world shows up, will you? And if they do, if you're based off the webserver, they'll see the correct information - basically if they visit when it's their middle of the day, and it's the middle of the night where you are, you might not want to show them a "call us here" link. And again - if you have control of your server, you have control of what time it shows. Regardless of that, you can control what time PHP shows - regardless of what time the SERVER has running - this is what we have timezone-settings in PHP for.

    Agreed, it might not be ideal, but it's perfectly fine for a plethora of different scenarios - since we know nothing of the OP's scenarios, giving a solution to what he asked for seemed pertinent.
     
    PoPSiCLe, Apr 11, 2015 IP