Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: PHP TIME() and birthday

  1. #1
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default PHP TIME() and birthday

    Is there a way to use time() on a website without relying on the user having the correct time set on their pc.


    Also for a registration form, if I had three options Date Month Year for them to fill in their birthday how coan I work out how old they are and when it's their birthday, update the sql database with their new age?

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

    Default

    time() works on the server, and on the server only. It doesn't matter what time their OS claims it is. This is good, but it also can be a problem if they are in a different timezone. You can use some complicated methods to determine a timezone offset potentially if needed.


    Birthdays are easy if you use timestamps. It's just some math to convert DD MM YYYY to a timestamp, then you can do any math you'd like, usually just by adding or subtracting a certain value, such as 60*60*24*365 (1 year). But there are other ways to manipulate timestamps using default functions. Look into this and you'll find your answers.
    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
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Smile hello

    Could anyone please give me a good website which has information about timestamps?

  4. #4
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Quote Originally Posted by djr33 View Post
    Birthdays are easy if you use timestamps. It's just some math to convert DD MM YYYY to a timestamp, then you can do any math you'd like, usually just by adding or subtracting a certain value, such as 60*60*24*365 (1 year).
    You're forgetting about leap years. You'd need a conditional to determine how many leap year years have passed, add them together, multiply those days by 60*60*24 and subtract that value from the original equation.

    You could attempt to convert a timestamp in DD MM YYYY format using strtotime(), which would give you a universal UNIX timestamp. You could also use strtotime() to subtract one year like strtotime("-1 year", <timestamp>);.
    - Josh

  5. #5
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    I just realized how unhelpful my reply was to the original post.

    Like Daniel said, time() is server-side only. It all depends on the server's time-keeping and the timezone in which the server is located.

    Converting a birthday in MM DD YYYY is pretty simple. Let's say the user, through drop-down menus, have entered the following data:
    Month: 11
    Day: 17
    Year: 1992

    Well, then add the strings together and convert it using strtotime(). It will convert it to a UNIX timestamp. Then you can just do timestamp / (60*60*24*365). Note that it doesn't count leap year days, so it will probably be a few days off unless the user is 4 years old. That requires a separate algorithm.

    For example:
    PHP Code:
    <?php

    $month 
    11;
    $day 17;
    $year 1992;

    $time strtotime("$month $day $year");

    $age $time / (60*60*24*365);

    echo 
    "You are $age years old!";

    ?>
    Would return "You are 18 years old!".
    - Josh

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

    Default

    You're right about the correction. I just skipped that to keep things basic. As I said, there are functions (such as strtotime()) that will help with doing more complicated mathematical operations and automating some of it.


    Here's plenty of information (including a few tutorials). Time isn't easy to start with, but it's not very complicated once you understand it. Remember, the time is just stored as a number of seconds since 1970. So that is an easy way to deal with "time", since you're just counting seconds.
    http://www.google.com/search?q=timestamps+in+php
    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

  7. #7
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Quote Originally Posted by JShor View Post
    I just realized how unhelpful my reply was to the original post.

    Like Daniel said, time() is server-side only. It all depends on the server's time-keeping and the timezone in which the server is located.

    Converting a birthday in MM DD YYYY is pretty simple. Let's say the user, through drop-down menus, have entered the following data:
    Month: 11
    Day: 17
    Year: 1992

    Well, then add the strings together and convert it using strtotime(). It will convert it to a UNIX timestamp. Then you can just do timestamp / (60*60*24*365). Note that it doesn't count leap year days, so it will probably be a few days off unless the user is 4 years old. That requires a separate algorithm.

    For example:
    PHP Code:
    <?php

    $month 
    11;
    $day 17;
    $year 1992;

    $time strtotime("$month $day $year");

    $age $time / (60*60*24*365);

    echo 
    "You are $age years old!";

    ?>
    Would return "You are 18 years old!".
    PHP Code:
    <?php

    $month 
    11;
    $day 17;
    $year 1992;

    $time strtotime("$month $day $year");

    $age $time / (60*60*24*365);

    echo 
    "You are $age years old!";

    ?>
    I tried that and it just came up with

    You are 0 years old!

    Any help?

  8. #8
    Join Date
    Mar 2011
    Posts
    2,144
    Thanks
    59
    Thanked 116 Times in 113 Posts
    Blog Entries
    4

    Default

    Also for a dropdown select so they can enter their age like this

    <option value="2010">2010</option>
    <option value="2009">2009</option>
    <option value="2008">2008</option>
    <option value="2007">2007</option>
    <option value="2006">2006</option>
    <option value="2005">2005</option>
    <option value="2004">2004</option>
    <option value="2003">2003</option>
    <option value="2002">2002</option>
    <option value="2001">2001</option>
    Thats for the year column but how do I let them select a month and then based on that display how many days e.g

    August - and it goes up to 31 days e.g
    Last edited by keyboard; 09-02-2011 at 06:48 AM. Reason: Cause I'm cool like that

  9. #9
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Whoops, I forgot the slashes. I also forgot the floor(). Try this:
    PHP Code:
    <?php

    $month 
    11;
    $day 17;
    $year 1992;

    $date "$month/$day/$year";
    $time strtotime($date);

    $age floor($time / (60*60*24*365));

    echo 
    "You are $age years old!";

    ?>
    - Josh

  10. #10
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    As for the drop-down menus, it would just be easier to populate the HTML using loops.

    PHP Code:
    <?php

    $month 
    = array('January''February''March''April''May''June''July''August''September''October''November''December');

    echo 
    " <select name=\"month\">";
    foreach(
    $month as $m) {
        echo 
    "<option value=\"$m\">$m</option>";
    }
    echo 
    "</select>";

    $d 0;

    echo 
    " <select name=\"day\">";
        while(
    $d 31) {
            
    $d++;
            echo 
    "<option value=\"$d\">$d</option>";
        }
    echo 
    "</select>";

    $y date("Y");

    echo 
    " <select name=\"year\">";
        while(
    $y >= date("Y"strtotime("-100 year"))) {
            echo 
    "<option value=\"$y\">$y</option>";
            
    $y--;
        }
    echo 
    "</select>";


    ?>
    - Josh

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
  •