jQuery / Moment到PHP日期时间转换

时间:2015-09-18 21:26:46

标签: javascript php jquery mysql time

这是朋友制作的jQuery / Moment Code

$(document).ready(function() {
      //get current time
      moment.tz.add('America/New_York|EST EDT|50 40|0101|1Lz50 1zb0 Op0');
      var currTime = moment().tz('America/New_York');
      $('.currTime').text(currTime.format("YYYY-MM-DD HH:mm:ss"));
      $('input[name="gmt_offset"]').val(new Date().getTimezoneOffset() / 60);

      //calculate window time
      var tblRows = $('tbody > tr').each(function() {

        var windowOpen = moment($(this).find('td.windowOpen').text());
        var windowClose = moment($(this).find('td.windowClose').text());
        var estTime = moment($('.currTime').text());
        if (parseInt(windowOpen.format("X")) > parseInt(estTime.format("X"))) {
          //Window Open Days
          if (parseInt(windowOpen.diff(estTime, "days")) > 0) {
            $(this).find('td.winOpenDay').text(windowOpen.diff(estTime, "days"));
          } else {
            $(this).find('td.winOpenDay').text("0").addClass("danger");
          }
          //Window Open Hours
          if (parseInt(windowOpen.diff(estTime, "hours")) < 24 && parseInt(windowOpen.diff(estTime, "hours")) > 0) {
            $(this).find('td.winOpenHr').text(windowOpen.diff(estTime, "hours"));
          } else if (parseInt(windowOpen.diff(estTime, "hours")) < 0 || parseInt(windowOpen.diff(estTime, "hours")) % 24 < 0 || parseInt(windowOpen.diff(estTime, "hours")) === 0) {
            $(this).find('td.winOpenHr').text("0").addClass("danger");
          } else if (parseInt(windowOpen.diff(estTime, "hours")) > 24) {
            $(this).find('td.winOpenHr').text(parseInt(windowOpen.diff(estTime, "hours")) % 24);
          }
          //Window Open Min
          if (parseInt(windowOpen.diff(estTime, "minutes")) < 60 && parseInt(windowOpen.diff(estTime, "minutes")) > 0) {
            $(this).find('td.winOpenMin').text(windowOpen.diff(estTime, "minutes"));
          } else if (parseInt(windowOpen.diff(estTime, "minutes")) < 0 || parseInt(windowOpen.diff(estTime, "minutes")) % 60 < 0 || parseInt(windowOpen.diff(estTime, "minutes")) === 0) {
            $(this).find('td.winOpenMin').text("0").addClass("danger");
          } else if (parseInt(windowOpen.diff(estTime, "minutes")) > 60) {
            $(this).find('td.winOpenMin').text(parseInt(windowOpen.diff(estTime, "minutes")) % 60);
          }
        } else {
          $(this).find('td.winOpenDay').text("0").addClass("danger");
          $(this).find('td.winOpenHr').text("0").addClass("danger");
          $(this).find('td.winOpenMin').text("0").addClass("danger");
        }

        if (parseInt(windowClose.format("X")) > parseInt(estTime.format("X")) && parseInt(windowOpen.format("X")) < parseInt(estTime.format("X"))) {
          //Window Close Hours
          if (parseInt(windowClose.diff(estTime, "hours")) < 24 && parseInt(windowClose.diff(estTime, "hours")) > 0) {
            $(this).find('td.winCloseHr').text(windowClose.diff(estTime, "hours"));
          } else if (parseInt(windowClose.diff(estTime, "hours")) < 0 || parseInt(windowClose.diff(estTime, "hours")) % 24 < 0 || parseInt(windowClose.diff(estTime, "hours")) === 0) {
            $(this).find('td.winCloseHr').text("0").addClass("danger");
          } else if (parseInt(windowClose.diff(estTime, "hours")) > 24) {
            $(this).find('td.winCloseHr').text(parseInt(windowClose.diff(estTime, "hours")) % 24);
          }
          //Window Open Min
          if (parseInt(windowClose.diff(estTime, "minutes")) < 60 && parseInt(windowClose.diff(estTime, "minutes")) > 0) {
            $(this).find('td.winCloseMin').text(windowClose.diff(estTime, "minutes"));
          } else if (parseInt(windowClose.diff(estTime, "minutes")) < 0 || parseInt(windowClose.diff(estTime, "minutes")) % 60 < 0 || parseInt(windowClose.diff(estTime, "minutes")) === 0) {
            $(this).find('td.winCloseMin').text("0").addClass("danger");
          } else if (parseInt(windowClose.diff(estTime, "minutes")) > 60) {
            $(this).find('td.winCloseMin').text(parseInt(windowClose.diff(estTime, "minutes")) % 60);
          }
        } else {
          $(this).find('td.winCloseHr').text("0").addClass("danger");
          $(this).find('td.winCloseMin').text("0").addClass("danger");
        }

      });

    });
.tableHeader{background-color:rgb(38,50,72);color:rgb(192,152,0);}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://momentjs.com/downloads/moment.min.js"></script>
<script src="http://momentjs.com/downloads/moment-timezone.min.js"></script>
<table class="table">
    <thead>
    <tr class="tableHeader">
        <td></td>
        <td class="text-left">Kill Info</td>
        <td class="text-center">Current Time (EST)</td>
        <td class="currTime">2015-09-18 17:03:27</td>
        <td colspan="3" class="text-center">Windows Open In</td>
        <td colspan="2" class="text-center">Window Closes In</td>
    </tr>
    <tr>
        <th>Target</th>
        <th>Death (EST)</th>
        <th>Window Opens (EST)</th>
        <th>Window Closes (EST)</th>
        <th>Days</th>
        <th>Hours</th>
        <th>Minutes</th>
        <th>Hours</th>
        <th>Minutes</th>
    </tr>
    </thead>
    <tbody>
    <tr class="Mob1">
        <td class="active">Mob1</td>
        <td class="deathTime warning">2015-09-18 14:18:51</td>
        <td class="active windowOpen">2015-09-19 14:18:51</td>
        <td class="active windowClose">2015-09-20 10:18:51</td>
        <td class="winOpenDay">0</td>
        <td class="winOpenHr">0</td>
        <td class="winOpenMin">0</td>
        <td class="winCloseHr">0</td>
        <td class="winCloseMin">0</td>
    </tr>
    <tr class="Mob2">
        <td class="active">Mob2</td>
        <td class="deathTime warning">2015-09-17 11:01:45</td>
        <td class="active windowOpen">2015-09-18 11:01:45</td>
        <td class="active windowClose">2015-09-19 07:01:45</td>
        <td class="winOpenDay">0</td>
        <td class="winOpenHr">0</td>
        <td class="winOpenMin">0</td>
        <td class="winCloseHr">0</td>
        <td class="winCloseMin">0</td>
    </tr>
    <tr class="Mob3">
        <td class="active">Mob3</td>
        <td class="deathTime warning">2015-09-18 05:27:01</td>
        <td class="active windowOpen">2015-09-19 05:27:01</td>
        <td class="active windowClose">2015-09-20 01:27:01</td>
        <td class="winOpenDay">0</td>
        <td class="winOpenHr">0</td>
        <td class="winOpenMin">0</td>
        <td class="winCloseHr">0</td>
        <td class="winCloseMin">0</td>
    </tr>
    </tbody>
</table>

我一直在尝试用PHP重新创建它,这就是我得到的

function diff_time($diff_timestamp1, $diff_timestamp2) {

    $diff_seconds  = $diff_timestamp1 - $diff_timestamp2;
    $diff_days     = floor($diff_seconds/86400);
    $diff_seconds -= $diff_days*86400;
    $diff_hours    = floor($diff_seconds/3600);
    $diff_seconds -= $diff_hours*3600;
    $diff_minutes  = floor($diff_seconds/60);

    return array('d' => $diff_days, 'h' => $diff_hours, 'm' => $diff_minutes);
}

<table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="16%" class="text_header">&nbsp;</td>
    <td width="16%" class="text_header">Kill Info</td>
    <td width="17%" class="text_header center_txt">Current Time (EST)</td>
    <td width="17%" class="text_header center_txt"><?php echo date('m-d-Y h:i A', time()); ?></td>
    <td width="20%" class="text_header center_txt">Windows Open In</td>
    <td width="14%" class="text_header center_txt">Window Closes In</td>
    </tr>
    <tr>
    <td width="16%" class="text_header">Target</td>
    <td width="16%" class="text_header">Death (EST)</td>
    <td width="17%" class="text_header">Window Opens (EST)</td>
    <td width="17%" class="text_header">Window Closes (EST)</td>
    <td width="20%" class="text_header">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td width="33%" class="text_header">Days</td>
            <td width="33%" class="text_header">Hours</td>
            <td width="33%" class="text_header">Minutes</td>
        </tr>
        </table>
    </td>
    <td width="14%" class="text_header">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td width="50%" class="text_header">Hours</td>
            <td width="50%" class="text_header">Minutes</td>
        </tr>
        </table>
    </td>
    </tr>
    <?php
    for ($i = 0; $i < $mobs["count"]; $i++) {
    read_times($mobs[$i]["kill_mob"], $times);
    switch($mobs[$i]["kill_mob"]){
        case "Mob1":
        case "Mob2":
        case "Mob3":
        case "Mob4":
        $mobWinOpen = "+ 16 hours";
        $mobWinClose = "+ 26 hours";
        break;
        case "Mob5":
        $mobWinOpen = "+ 40 hours";
        $mobWinClose = "+ 60 hours";
        break;
        default:
        $mobWinOpen = "+ 24 hours";
        $mobWinClose = "+ 44 hours";
        break;
    }
    $winopen = strtotime($mobWinOpen, $times["kill_time"]);
    $winclose = strtotime($mobWinClose, $times["kill_time"]);
    $open_time  = diff_time($winopen, time());
    $close_time = diff_time($winclose, time());
    ?>
    <tr>
        <td class="text_normal"><?php echo $times["kill_mob"]; ?></td>
        <td class="text_normal"><?php echo date('m-d-Y h:i A', $times["kill_time"]); ?></td>
        <td class="text_normal"><?php echo date('m-d-Y h:i A', $winopen); ?></td>
        <td class="text_normal"><?php echo date('m-d-Y h:i A', $winclose); ?></td>
        <td class="text_normal">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
            <?php
            if ($winopen > time()) {
                ?>
                <td width="33%" class="text_normal">
                <?php
                if ($open_time["d"] > 0) {
                    echo $open_time["d"];
                } else {
                    echo "0";
                }
                ?>
                </td>
                <td width="33%" class="text_normal">
                <?php
                if (($open_time["h"] < 86400) && ($open_time["h"] > 0)) {
                    echo $open_time["h"];
                } elseif (($open_time["h"] < 0) || ($open_time["h"] % 86400) < 0 || ($open_time["h"] == 0)) {
                    echo "0";
                } elseif ($open_time["h"] > 86400) {
                    echo $open_time["h"] % 86400;
                }
                ?>
                </td>
                <td width="33%" class="text_normal">
                <?php
                if (($open_time["m"] < 60) && ($open_time["m"] > 0)) {
                    echo $open_time["m"];
                } else if (($open_time["m"] < 0) || ($open_time["m"] % 60 < 0) || ($open_time["m"] == 0)) {
                    echo "0";
                } else if ($open_time["m"] > 60) {
                    echo $open_time["m"] % 60;
                }
                ?>
                </td>
            <?php
            } else {
                ?>
                <td width="33%" class="text_normal">0</td>
                <td width="33%" class="text_normal">0</td>
                <td width="33%" class="text_normal">0</td>
            <?php
            }
            ?>
            </tr>
        </table>
        </td>
        <td class="text_normal">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
            <?php
            if (($winclose > time()) && ($winopen < time())) {
                ?>
                <td width="50%" class="text_normal">
                <?php
                if (($close_time["h"] < 86400) && ($close_time["h"] > 0)) {
                    echo $close_time["h"];
                } elseif (($close_time["h"] < 0) || ($close_time["h"] % 86400) < 0 || ($close_time["h"] == 0)) {
                    echo "0";
                } elseif ($close_time["h"] > 86400) {
                    echo $close_time["h"] % 86400;
                }
                ?>
                </td>
                <td width="50%" class="text_normal">
                <?php
                if (($close_time["m"] < 60) && ($close_time["m"] > 0)) {
                    echo $close_time["m"];
                } else if (($close_time["m"] < 0) || ($close_time["m"] % 60 < 0) || ($close_time["m"] == 0)) {
                    echo "0";
                } else if ($close_time["m"] > 60) {
                    echo $close_time["m"] % 60;
                }
                ?>
                </td>
            <?php
            } else {
                ?>
                <td width="50%" class="text_normal">0</td>
                <td width="50%" class="text_normal">0</td>
            <?php
            }
            ?>
            </tr>
        </table>
        </td>
    </tr>
    <?php
    }
    ?>
</table>

我的代码没问题,它会输出正确的“死亡”,“窗口打开”和“窗口关闭”时间。问题似乎是每个暴徒的天/小时/分钟和小时/分钟。我尝试在PHP中重新创建jQuery / Moment代码,但它只是不起作用,我无法弄清楚是什么导致它陷入困境。

0 个答案:

没有答案