www.webdeveloper.com
Results 1 to 3 of 3

Thread: date difference

  1. #1
    Join Date
    Sep 2008
    Posts
    206

    date difference

    now i have this form and i want to get todaysdate in the text box named today_date

    after that i want the user to enter a date in entered_date and i want to compare this date with today_date and show the answer

    i dont know how to make it using php

    i need a full help wiz code can anyone helps me??



    <table width="600" border="0" cellspacing="0" cellpadding="0">
    <form id="1" name="1" method="get" action="test1">
    <tr>
    <td width="40%"><label for="today_date">Today Date <br>
    <span class="style1">( Date format is dd-mm-yyyy)</span></label></td>
    <td width="20%" align="center" valign="middle">&nbsp;</td>
    <td width="50%"><input type="text" name="today_date" id="today_date" readonly onblur="return datediff();" /></td>
    </tr>
    <tr>
    <td><label for="entered_date">Entered Date<br>
    <span class="style1">( Date format is dd-mm-yyyy)</span></label></td>
    <td align="center" valign="middle">&nbsp;</td>
    <td><input type="text" name="entered_date" id="entered_date" onblur="return datediff();"/></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>Date Difference</td>
    <td>&nbsp;</td>
    <td><label for="difference"></label>
    <input type="text" name="difference" id="difference" readonly /></td>
    </tr>
    </form>
    </table>

  2. #2
    Join Date
    Aug 2007
    Location
    Washington
    Posts
    219
    Well it almost looks to me like you want this to be done with JS because of that onblur but here's how you would do it with PHP. This all would be done in "action" page of the form.

    I do suggest you ask in the javascript forum however.

    PHP Code:

    $date1 
    strtotime($_GET['entered_date']);
    $date2 strtotime($_GET['today_date']);

    $dateDiff $date1 $date2;
    $fullDays floor($dateDiff/(60*60*24));
    $fullHours floor(($dateDiff-($fullDays*60*60*24))/(60*60));
    $fullMinutes floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
    echo 
    "Differernce is $fullDays days, $fullHours hours and $fullMinutes minutes."
    Edit: For an example of what I think you want check here: http://javascript.internet.com/math-...ifference.html
    Last edited by bejitto101; 12-11-2008 at 04:57 AM.

  3. #3
    Join Date
    Oct 2008
    Posts
    150
    You can also do it like this with PHP:
    PHP Code:
    function date_time_diff($start$end$date_only true)  // $start and $end as timestamps
    {
        if (
    $start $end) {
            list(
    $end$start) = array($start$end);
        }
        
    $result = array('years' => 0'months' => 0'days' => 0);
        if (!
    $date_only) {
            
    $result array_merge($result, array('hours' => 0'minutes' => 0'seconds' => 0));
        }
        foreach (
    $result as $period => $value) {
            while ((
    $start strtotime('-1 ' $period$start)) >= $end) {
                
    $result[$period]++;
            }
            
    $start strtotime('+1 ' $period$start);
        }
        return 
    $result;
    }

    $date_1 strtotime('2005-07-31');
    $date_2 time();
    $diff date_time_diff($date_1$date_2);
    foreach (
    $diff as $key => $val) {
        echo 
    $val ' ' $key ' ';
    }

    // Displays:
    // 3 years 4 months 11 days 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles