Results 1 to 8 of 8

Thread: PHP help using dates/times

  1. #1
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP help using dates/times

    Hi I am trying to run a PHP script to display a certain image at a set time of the day for 1 minute, and display another image for all other times. I assumed that the time (H:i) needs to be entered as UTC time however the image is not changing when I would expect it to. Below is a copy of what I've been working with. Here's hoping someone can identify my error. Thanks.

    PHP Code:
    <?php

    $datetime 
    date("H:i");

    if ( 
    $datetime == "11:10" ) {
          
    $im imagecreatefromgif("image2.gif");
    } else {
          
    $im imagecreatefromgif("image1.gif");
    }

    header("Content-Type: image/gif");
    imagegif($im);
    imagedestroy($im);


    ?>
    Last edited by traq; 06-20-2013 at 08:06 PM. Reason: please use [code]/[php] tags

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Moderator's note: please try to use more descriptive titles when posting. You're in the PHP help section, so we know you want help with PHP. In this case, you should probably have mentioned something about using dates/times in PHP. That will actually benefit you-- more users will view your post and answer your question because they know what it's about, plus the fact that others can later find the discussion if they have similar questions.


    There's one problem with this: PHP only determines what happens when the page loads (it just generates an HTML page), so you can have PHP show one image at one time of day, but it won't disappear after that minute. I don't know if this is important to you. (Note that in theory you would use Javascript to hide it after that one minute, but it wouldn't be secure, because unlike PHP, Javascript operates within the browser and can be modified by the user if they know what they're doing or take the time to figure it out.)

    Your code looks fine to me. Have you tested it to be sure that the image commands are working? That's the first step.

    Next:
    http://www.php.net/manual/en/function.date.php
    Your use of H:i looks fine. What this means is that at 11:10am at server time you will display one image. Any other time, you'll display another.

    It's technically possible to create code that will find the UTC offset and calculate the difference for you. However, in this case (unless you want the code to be portable for other machines) it will be simpler to just use trial and error to figure out where the machine is.

    Start with this:
    echo date("H:i");
    Then compare that to your local time and/or UTC, and you'll know where the server is located (or what timezone it is set to-- probably where it's located). Then just add/subtract the right number of hours (the minutes should be ok), and the code should work.

    Note: when using 'echo' you won't see anything if you have an image being displayed (and/or it will cause errors on the page and no image to be shown). For that reason, you should probably comment out the end of your code while testing. And instead, you can replace the whole thing with code like this:
    PHP Code:
    <?php

    $datetime 
    date("H:i");


    echo 
    $datetime;


    if ( 
    $datetime == "11:10" ) {
    //$im = imagecreatefromgif("image2.gif");
    echo 'image2';
    } else {
    //$im = imagecreatefromgif("image1.gif");
    echo 'image1';
    }

    /*header("Content-Type: image/gif");
    imagegif($im);
    imagedestroy($im);*/


    ?>
    (Also, note for the future that you can use [php] code tags to format it nicely for us here, or just [code] for other kinds of code.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    djr33 many thanks for the help and the explanation.

  4. #4
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    djr33 I ran the code as suggested and it returned a timezone that was more or less GMT but in actual fact 4 minutes fast. I am thinking it may be easier if I use my local timezone for the code. How how I go about adjusting my code above to display image2.gif for 1 minute based on my local timezone rather than the default timezone?

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You can't. The only clock available to your server is your server's clock, which is set to whatever the server administrator thinks is best (and for whatever timezone is best, probably whatever is local for the server). You could change that if you had admin level access to the server, but I'm guessing you don't.
    It's odd that it's 4 minutes fast. Most servers I've used have been accurate-- that's fairly important. You might want to consider switching hosts if they can't even maintain the clock. Or just contact tech support. Maybe they'll fix it immediately.
    (And, obvious question: are you sure that their clock is fast rather than yours being slow? Just double check that before you do anything. But I assume you have.)

    What can you do about it? Simple. Just subtract hours/minutes as needed. So you can subtract four minutes if you want. You can also check the seconds. You can synchronize it with anything. One important question then is why the server is set 4 minutes fast-- if that's just a typo (or something like that), then don't worry about it-- that's actually a good thing, and it will be reliable. But if that's because the clock isn't accurate and it's becoming (for example) increasingly inaccurate, your math corrections won't work for long. In that case, you probably do need a new host (or to see if tech support can move you to a better physical server).


    Some thoughts more generally:
    1. You can use local time via Javascript, but there are a few problems with this: 1) it's not secure (so users can choose what time they'd like it to be, so to speak); 2) it's complicated to set up (you need to get the time from Javascript to PHP, which involves at the very least loading one previous page, and it won't necessarily always work); and 3) it would vary by user-- the information must come from a specific user (whoever is visiting) so you can't synchronize them (and this in turn hints that you're using their time and lets them know they can manipulate it with their system's clock).
    2. You could look into some kind of serverside synchronization program. I imagine something like that exists, maybe for scientists who care more specifically about the exact time. Search for something like "PHP atomic clock API" and see if you can find a way to grab a time value from an external source and use that. Then you can bypass whatever your host is doing. It would be a little complicated (and it shouldn't be necessary) but it should work for you and it would also be more likely to line up with what your visitors expect if they go to the same kind of trusted source for the information. Alternatively you could make it interesting by choosing a specific clock, like Big Ben or something along those lines, assuming there's an API out there for that clock.
    Technically you could make that your computer's local clock, but that would require 1) your computer always being online; 2) creating your own API (so to speak); and 3) making your computer into a server with a reliable IP or domain so you can access it. Or, a hybrid solution would involve something like this: synchronize your computer and the server once a day; for the rest of the day, figure out your local time by doing math based on those two original data points (stored in a database or elsewhere). That is, you know it has been 10,000 seconds since the server's original time (based on current-minus-original values for the server) then add 10,000 to your original local time, and now you know/approximate your local time. In fact, this is exactly equivalent to the original idea above, except that it would keep it synchronized every day in case either clock is slow/fast, losing a second or two each time (rarely would it be more than that).
    3. I'm guessing you're making this for some kind of game, where visitors should come at exactly 11:10 am or whatever. But an easy solution to make your job a little easier would be to make the window just a little longer, maybe 5 minutes instead of 1. You could even make sure that for each user it only appears for one minute (if they reload the page one minute later, it would, artificially, show them the other image), but that for all users it appears within that 5 minutes if it's the first time they've visited the page. You could control the per visitor settings by using PHP sessions or cookies (note: sessions and cookies aren't secure, but in this case I doubt it would be a major security problem if they figured out how to bypass it for the rest of the 5 minutes).


    If you need more help than that, you should add some specifics about exactly what effect you're trying to accomplish (and why).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    You can actually use date_default_timezone_set() to tell PHP what timezone you want dates/times converted to.

    HOWEVER, this is probably a bad idea. First, it affects all dates/times: even ones your user submits - so, if you set your default timezone ID to "America/Los_Angeles" and someone from Moscow sends you the time "11:30PM", PHP will assume that that means "11:30PM Pacific Time." Second, all other issues aside, it is no better or worse than not knowing the timezone at all, since it will be wrong for 23/24ths of the world anyway.

    A better approach is to make sure your server is set to use UTC internally, and then you can use javascript to format your times to the user's timezone.

    As Daniel says, we need more info about what you hope to accomplish. Is it critical to use local time? Is it important to allow the alternate image for only that one minute every 24 hours?


    ----------
    Also, as far as your serving the image goes, there's a simpler (and much more efficient way) to do it:
    PHP Code:
    <?php

    $datetime 
    date("H:i");

    if ( 
    $datetime == "11:10" ) {
          
    $im "image2.gif";
    } else {
          
    $im "image1.gif";
    }

    header"Content-Type: image/gif" );
    readfile$im );
    Last edited by traq; 06-20-2013 at 08:29 PM.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You can actually use date_default_timezone_set() to tell PHP what timezone you want dates/times converted to.

    HOWEVER, this is probably a bad idea. First, it affects all dates/times: even ones your user submits - so, if you set your default timezone ID to "America/Los_Angeles" and someone from Moscow sends you the time "11:30PM", PHP will assume that that means "11:30PM Pacific Time." Second, all other issues aside, it is no better or worse than not knowing the timezone at all, since it will be wrong for 23/24ths of the world anyway.
    Correct. That's all possible. However, I didn't mention it above for two reasons:
    1) Manually subtracting the time should be enough in this case. (Although I agree that changing the server to UTC is best if you can do that.)
    2) It will probably run into exactly the same problems as the method I described, such as the 4-minutes-fast issue.


    Anyway, again it depends on what you want to do. There are lots of options, so I hope what we've said is helpful. Traq's ideas are, indeed, the "right" way to do it probably.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    Right - to clarify, Jason F, I am not suggesting date_default_timezone_set() as a solution to your problem.

    If you want to implement my suggestion and ensure that your scripts use UTC (and cannot affect the change at the server level), then date_default_timezone_set( 'UTC' ); would be a good solution.

    (I also agree that the "4 minutes fast" issue is "weird" and really shouldn't be happening.)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •