Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By Dubz
  • 1 Post By mlseim

Thread: PHP Script to detect Month and Time of Day

  1. #1
    New Coder
    Join Date
    Sep 2011
    Location
    Bournemouth
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts

    PHP Script to detect Month and Time of Day

    Hello, I am attempting to achieve a script that:

    1) Detects the month of the year and outputs an echo statement based on whether the month == August or not.
    2) Detects whether the time is before midday or after and then outputs an echo statement below.
    3) Otherwise checks whether the time is after midday but before 3pm and then outputs a different echo statement below.
    4) Or otherwise outputs an echo statement that it is before 12pm and therefore it is cooler outside in the summer months.

    This is my code so far (with some from an exercise website towards the start but slightly adjusted based on what I read at W3C:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
      
    <html xmlns="http://www.w3.org/1999/xhtml"  xml:lang="en" lang="en"> 
    <head> 
    <meta http-equiv="content-type" content="text/html;charset=iso-8859-1" /> 
    <title>If, Else, Elseif Statement</title> 
    </head> 
       
    <body> 

    <?php

    $date 
    date("F");

    if         (
    $date == "August") {
    echo 
    "<p>It's August so it is really hot. </p>"; }


    else {
    echo 
    "<p>It's not August so at least it is not the hottest month of the year.</p>";
    }

    echo 
    "<p><strong>I decided to add this bit on to make the script more informative..</strong></p>";

    $hour date("h");
    $minute date("i");

    if (
    $hour <= "12") {

    echo 
    "<p>It's before midday anyway so it will be cooler.</p>";

    else {
    echo 
    "<p>The time is $hour : $minute so, until 15:00 it is going to be much hotter during the summer period.</p>"
    }


    ?>

    </body>
    </html>
    Any help would be gratefully received with working out where I am going wrong.

    Thanks and Kind Regards,

    Beanie.

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Your first issue might be the fact that PHP's date() function is based off the server's time and timezone settings, not the client.

    If you need to use the clients, you'll have to somehow obtain their timezone, either by a javascript hidden post with it (scans time settings and sends to server to save as a cookie) or their selection (ex. dropdown list). After that you would need to look to convert the time. If you only want to display it to them and don't need server actions to happen then look into using javascript for this, not PHP.

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,537
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    This sort of looks like a homework assignment for a programming class.

    Is the script only for you, and your timezone? What if someone on the other side of the world runs your script?

    If this is homework, we can give you hints.
    bgareth likes this.

  • #4
    New Coder
    Join Date
    Sep 2011
    Location
    Bournemouth
    Posts
    20
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Dubz - Hello and thanks for these pointers - Similar pointers regarding Client use of the script from mlseim - thanks to!

    I'm just starting to get back into php as I have really struggled previously. I'd like to make some money out of website development as I am getting a bit short!

    At this stage, I am following some online exercises which can be found at Beginner to Intermediate PHP Exercises | PHP Exercises They are currently down as too many people are connected! I am a total newbie!

    I have extended the exercise myself as a challenge as I wanted to use elseif to!

    I've managed to get my script working as follows after I posted but I was still interested in your feedback.

    Here is my working code:

    PHP Code:
    <?php

    $date 
    date("F");

    if         (
    $date == "August") {
    echo 
    "<p>It's August so it is really hot. </p>"; }


    else {
    echo 
    "<p>It's not August so at least it is not the hottest month of the year.</p>";
    }

    echo 
    "<p><strong>I decided to add this bit on to make the script more informative..</strong></p>";

    $hour date("H");
    $minute date("i");

    if (
    $hour <= "12") {

    echo 
    "<p>It's before midday anyway so it will be cooler.</p>"; }

    elseif (
    $hour <= "15") {
    echo 
    "<p>The time is $hour:$minute so, until 15:00 it is going to be much hotter during the summer period.</p>"
    }

    else {
    echo 
    "<p>It will be a little cooler than between 12:00 and 15:00.</p>";
    }


    ?>
    I'm also struggling with getting the terminology in this script write: functions, arguments and other terminology so any feedback in this regard would also be gratefully received.

    Thanks again,

    Beanie.

  • #5
    New Coder
    Join Date
    May 2014
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Use the function int strtotime ( string $time [, int $now = time() ] )


  •  

    Posting Permissions

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