Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
  1. #1
    Senior Member
    Join Date
    Mar 2004
    Posts
    110
    Member #
    5461
    Hi guys....

    I have a quick question.

    I want to set up a quick page on my website where people can add the date of their driving test... it will add it to the list of others and then show how many days are left until the driving test for that certain person.

    E.g -

    dan - 1 day
    elliot - 1 week 2 days
    emma - 3 days



    if possible, I would like it to auto-arrange in order of soonest first - if not, that is not a problem though.

    The fields I would need are
    Name - Location of test - date of test - time until test.

    Many thanks for the help.

    Amnesia

  2.  

  3. #2
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    To get the difference between 2 dates.. use this code.
    PHP Code:
    <?php
    // hours, minutes, seconds, month, day, year
    $dateDiff mktime(12,0,0,04,20,2003) - mktime(11,0,0,04,20,2003);
    ?>
    To get days difference from that..
    PHP Code:
    echo "Days to test: "floor($dateDiff/60/60/24)."."
    Then you would just insert that into your database table and order by column_name, where column name is the name of the column that holds the days to test.

  4. #3
    Senior Member
    Join Date
    Mar 2004
    Posts
    110
    Member #
    5461
    Thanks for the reply..
    So how would I go about setting all this up?

  5. #4
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Hey. http://bfsog.co.uk/testing/datecheck.php

    The code:

    PHP Code:
    <html>

    <head>

    <title>Get the difference between 2 dates</title>

    </head>

    <body>

    <?php

    if($_POST['submit']=="Get Difference") {

    //get todays date
    $today date("d/m/y");
    // I assume the user enters the date of the test?
    $test_date $_POST['test_date'];
    // format would be dd/mm/yy so explode it.
    $test_date explode("/"$test_date);
    // explode $today
    $today explode("/"$today);
    // get the difference
    $dateDiff mktime(0,0,0,$test_date[1],$test_date[0],$test_date[2]) - mktime(0,0,0,$today[1],$today[0],$today[2]);
    echo 
    "Days: ".floor($dateDiff/60/60/24).".";

    } else { 

    ?>

    <form name="getdiff" method="post" action="<?php echo $PHP_SELF?>">

    Input Date: <br />

    <input type="text" name="test_date" value="" maxlength="50" />

    <br /><br />

    <input type="submit" name="submit" value="Get Difference" />

    </form>

    <?php

    }

    ?>

    </body>

    </html>
    Now you just need to insert that into mysql. If you need help with that reply.

  6. #5
    Senior Member
    Join Date
    Mar 2004
    Posts
    110
    Member #
    5461
    Uhm, yes I think I will need help witht he mysql too

  7. #6
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Okay..
    I will assume your table is called user_dates

    PHP Code:
    <html>

    <head>

    <title>Get the difference between 2 dates</title>

    </head>

    <body>

    <?php

    if($_POST['submit']=="Get Difference") {

    // connect to mysql

    $location "localhost";
    $username "database_username_goes_here";
    $password "password_goes_in_here";
    $database "database_name_in_here";
    $conn mysql_connect("$location","$username","$password");
    if (!
    $conn) die ("Could not connect MySQL");
        
    mysql_select_db($database,$conn) or die ("Could not open database");

    //get todays date
    $today date("d/m/y");
    // get the form submitted vars
    $fullname $_POST['full_name'];
    $test_location $_POST['test_location'];
    $date $_POST['test_date'];
    $test_date $_POST['test_date'];
    // format would be dd/mm/yy so explode it.
    $test_date explode("/"$test_date);
    // explode $today
    $today explode("/"$today);
    // get the difference
    $dateDiff mktime(0,0,0,$test_date[1],$test_date[0],$test_date[2]) - mktime(0,0,0,$today[1],$today[0],$today[2]);
    $test_days floor($dateDiff/60/60/24);
    // do the query
    $insert_q "INSERT INTO user_dates (name, test_location, test_date, test_days) VALUES('$fullname','$test_location','$date',' $test_days')";
    mysql_query($insert_q) or die ("Could not add data to the table");
    if(
    mysql_affected_rows()==1) {
       echo 
    "Row added successfully. The query that ran was $insert_q";
    }

    } else {

    ?>

    <form name="getdiff" method="post" action="<?php echo $PHP_SELF?>">

    Pupil Name: <br />

    <input type="text" name="full_name" maxlength="50" />

    <br /><br />

    Test Location: <br />

    <input type="text" name="test_location" maxlength="50" />

    <br /><br />

    Input Date: <br />

    <input type="text" name="test_date" value="" maxlength="50" />

    <br /><br />

    <input type="submit" name="submit" value="Get Difference" />

    </form>

    <?php

    }

    ?>

    </body>

    </html>

  8. #7
    Senior Member
    Join Date
    Mar 2004
    Posts
    110
    Member #
    5461
    Hiya,
    Many thanks for the code.. however it says could not add date to database. Is there a certain format the date has to be entered in?

    Also - can I turn it into drop down boxes? So no dodgy dates will be entered...

    After the main coding has been done will it be possible to have two pages...

    The first page will hold all the dates in order... then at the bottom "add your test".. you click "add your test" which will take you to the form... then you submit your data and it will be entered onto the main page?

    Thanks again
    Dan

  9. #8
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    To see the mysql error you could change
    PHP Code:
    mysql_query($insert_q) or die ("Could not add data to the table"); 
    To
    PHP Code:
    mysql_query($insert_q) or die(mysql_error()); 
    That will show the error.

    Check your column names to match what I name them in the query.

    Try that and post the outcome.

    It is most likely a table name/column name issue, but mysql_error() will help.

    Post back your results.

  10. #9
    Senior Member
    Join Date
    Mar 2004
    Posts
    110
    Member #
    5461
    Hiya,
    It says
    Table 'dandan_userdates.user_dates' doesn't exist

    I haven't set anything up in mySQL apart from adding the database, the username and password...

  11. #10
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    Sorry I took long to reply, I was at a gig last night.

    You will need to create the table user_dates (or name it whatever but you will have to amend my code to match yours).

    Do you have phpMyAdmin?

    If so that tool can create the table for you.

    Open phpMyAdmin, click on a database from the list on the left.
    At the top of the page there will be a horizontal collection of buttons/images, click the SQL button.

    Enter this code.

    PHP Code:
    CREATE TABLE `user_dates` (
    `
    idINT10 UNSIGNED NOT NULL AUTO_INCREMENT ,
    `
    pupil_nameVARCHAR50 NOT NULL ,
    `
    test_locationVARCHAR50 NOT NULL ,
    `
    test_dateVARCHAR20 NOT NULL ,
    `
    test_daysVARCHAR10 NOT NULL ,
    PRIMARY KEY ( `id` )
    TYPE MYISAM 
    Once thats done click Go and if the table was created correctly the script should now work.

    Post back if helps needed.


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 11:45 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com