jQuery比较数组输入字段

时间:2014-07-22 08:30:08

标签: javascript php jquery arrays

我正在创建一个Weekly Timeslot。到目前为止,我已经完成了所有功能。但我需要比较时间。我不知道该怎么做。请帮助..

<table class = "widefat">
    <thead>
        <tr>
            <th>Day of Week</th>
            <th>Office Hours</th>
            <th>Lunch Time</th>
            <th>Day off</th>
        </tr>
    </thead>
        <?php
        $days = array(
                      "1" => "SUN",
                      "2" => "MON",
                      "3" => "TUE",
                      "4" => "WED",
                      "5" => "THU",
                      "6" => "FRI",
                      "7" => "SAT");
        foreach($days as $key => $value){
            $val=$value;
            $obj = data($val);
        ?>
            <tr>
                <td>
                    <?php echo $value ;?><input type ="hidden" name ="day[<?php echo $key ?>]" value=<?php echo $value; ?> class="day_week"/>
                </td>
                <td>
                    From <input type = "text" name="wk_from[<?php echo $key ?>]" id="office_hours_from_<?php echo $key; ?>" class ="working_from" value="<?php echo $obj['office_hour_from']; ?>" style="width:6em">
                    To   <input type = "text" name="wk_to[<?php echo $key ?>]" id="office_hours_to_<?php echo $key; ?>" class ="working_to" value="<?php echo $obj['office_hour_to']; ?>" style="width:6em">
                </td>
                <td>
                    From <input type = "text" name="lunch_from[<?php echo $key ?>]" id="lunch_hours_from_<?php echo $key; ?>" class ="break_from" value="<?php echo $obj['break_hour_from']; ?>" style="width:6em">
                    To   <input type = "text" name="lunch_to[<?php echo $key ?>]" id="lunch_hours_to_<?php echo $key; ?>" class ="break_to" value="<?php echo $obj['break_hour_to']; ?>" style="width:6em">
                </td>
                <td>
                    <?php
                        if($obj['day_off']==1){ ?>
                            <input type = "checkbox" name ="day_off[<?php echo $key ?>]" value="1" id="<?php echo $key; ?>" checked="checked" class="check_day_off">
                        <?php }else{ ?>
                            <input type = "checkbox" name ="day_off[<?php echo $key; ?>]" value="1" id="<?php echo $key; ?>"  class="check_day_off">
                        <?php   }
                    ?>
                    <input type ="hidden" name="doc_id" value="<?php echo get_current_user_id; ?>">
            <!--    <input type = "checkbox" name ="day_off[]" value" id="<?php //echo $key; ?>" class="check_day_off"?>-->
                </td>
            </tr>
    <?php   
    }

        ?>

</table>

在这里,我必须比较OfficeHours的TO时间必须大于From time。我如何才能在每天进行这种比较。将在jquery中完成吗?请帮助。谢谢提前..

2 个答案:

答案 0 :(得分:0)

只需使用此代码来比较php中的日期,我希望它可以帮助您:)

if( strtotime($date1) > strtotime($date2) )
{   
    echo "Date 1 is Greater than date2"; 
}

在像这样的JavaScript中

if ( Date.parse('01/01/2011 10:20') > Date.parse('01/01/2011 5:10') )
{
   //do something
}

1月是任意日期

答案 1 :(得分:0)

我假设您要在每日的基础上进行检查,以下代码应该有效:

$("#compare_time").click(function(){
    //take the hours and minutes
    var time_1 = $("#office_hours_from").val().substring(0,5).split(":");
    var time_2 = $("#office_hours_to").val().substring(0,5).split(":");

    //convert it to minutes
    var time_minute_1 = time_1[0] * 60 + time_1[1];
    var time_minute_2 = time_2[0] * 60 + time_2[1];

    //if it's "pm" then add 12 hours to the minute variable
    if($("#office_hours_from").val().substring(5,2) == "pm")
        time_minute_1 += 12 * 60;
    if($("#office_hours_to").val().substring(5,2) == "pm")
        time_minute_2 += 12 * 60;

    if(time_minute_2 >= time_minute_1)
        alert("valid");
    else
        alert("not valid");
});

查看此Fiddle ...