DateTime Picker

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • brendanmcdonagh
    New Member
    • Nov 2007
    • 153

    DateTime Picker

    Hi all,

    I ve discovered the power of php/mysql/html in last few days whilst putting together an online app to enable the staff of a friends company to log on and enter their current location and time due to leave current location.

    So far so good, they can select their name from a drop down box, enter location and time leaving which is then stored in a database to be loaded in another page.

    Now, one of the requests of my friend was to have the ability to scan the page and see if anyone is overdue - current time vs time leaving.

    So I have tried and thought of many different approaches to acheive what i was once acheived with datediff - vb but can't seem to find a solution. If only it was as easy as currentTime - timeLeaving . But it's not.

    I know javascript has something similar to datediff but can you acheive tis with php?

    Any directions would make my life easier??!!

    B
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Brendan.

    To calculate the difference between two dates, convert them into timestamps using the strtotime() function (PHP: strtotime - Manual), then subtract them. The result will be the number of seconds between the dates.

    E.g.,:
    [code=php]
    $date1 = '2009-01-21';
    $date2 = '2009-02-01';

    $secondsBetween Date1AndDate2 = strtotime($date 2) - strtotime($date 1);

    /* 86400 seconds in a day, so divide by that to get the # of days. */
    $daysBetweenDat e1AndDate2 = (int) ($secondsBetwee nDate1AndDate2 / 86400);
    [/code]

    Comment

    • brendanmcdonagh
      New Member
      • Nov 2007
      • 153

      #3
      Hi there

      The problem with the above solution is having to get the user to manually insert datat and time in html form, very unpredictable. I can have a now() function to insert current timestamp in an mysql field, in fact I have done that.

      But, I have got to get the time and date of when the user is due to leave current location in a Time Leaving: <input type="text" name="time"> with html.

      The best I can hope for is getting them to enter 22:05 but then this isn't compatible with my datadiff hopes. I 've tried. I know now it's going to have to be timestamp in mysql and timestamp in html/php but to get them to enter manually in the format needed is out of the question. That's why a date/time picker is needed.

      So my work/question now involves:

      How can i get a date/time picker - such as a drop down box for year, month, day, hh, mm.

      Then i'm in a better position to work with the timestamp from mysql - (now() and the date/time entered by the user to find the difference.

      Comment

      • brendanmcdonagh
        New Member
        • Nov 2007
        • 153

        #4
        Hi again,

        I have decided to try the following:

        have a table in my database for each of the following:

        day, month, h, mm

        then im showing these values in seperate drop down boxes on my page and posting inputs to insert2.php

        the following is working

        Code:
        $day = $_POST['day'];
        $month = $_POST['month'];
        $hour = $_POST['hour'];
        $minutes = $_POST['minutes'];
        $alltogether = $day . " " . $month . " " . $hour . ":" . $minutes . " ";

        my output being 27 January 16:30

        As im asking I m thinking it's a long shot but is there any way to compare this with current time to determine if &alltogether is past current time?

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Hi.

          Consider using <select> boxes for the date/time input.
          Your user would simply be presented with a drop-down box for the year, month and day, and either a drop-down for the time to or simply a text input for the time.
          [code=html]
          <select name="date_year ">
          <option value="2009">20 09</option>
          <option value="2010">20 10</option>
          <!-- etc... -->
          </select>
          <select name="date_mont h">
          <option value="01">Janu ary</option>
          <option value="02">Febr uary</option>
          <!-- etc... -->
          </select>
          <!-- etc... -->[/code]
          That way you would be able to assume a fairly accurate date input from your users.

          And to get the "overdue" times, if you have all the dates in your database in DATE, DATETIME or TIMESTAMP columns, you could have a SQL query fetch all the overdue rows. For example:
          [code=mysql]
          SELECT * FROM myTable
          WHERE timeToLeave < NOW();[/code]
          Which should return only rows where the "timeToLeav e" value is in the past.

          Comment

          Working...