为什么我的ajax时钟仅显示AM,而不显示AM和PM?

时间:2018-11-27 07:05:27

标签: javascript php html ajax

我有一个获取服务器时间的ajax时钟。它可以正确获取时间,但是似乎无法区分AM和PM。时钟是12小时制而不是24小时制。无论一天中的任何时间,它始终显示AM。

这是我的javascript:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
            <script type="text/javascript">
                var start_time;
                var current_time;

                //gets current server time
                var get_time = function () {
                    $.ajax({
                        type: 'GET',
                        url: '../Server_Side/clock.php',
                        data: ({ action : 'get_time' }),
                        success: (function (data) {
                            start_time = new Date(
                                data.year,
                                data.month,
                                data.day,
                                data.hour,
                                data.minute,
                                data.second
                           //comment
                            );
                            $('#clock').html();
                        }),
                        dataType: 'json'
                    });
                }

                //counts 0.25s
                var cnt_time = function () {
                    current_time = new Date(start_time.getTime() + 250);
                    $('#clock').html(current_time.toLocaleTimeString("en-US", {timeZone:'America/Vancouver'}));
                    start_time = current_time;
                }

                setInterval(cnt_time, 250); //add 250ms to current time every 250ms
                setInterval(get_time, 30250); //sync with server every 30,25 second
                get_time();
            </script>

这是我在服务器上的php时钟:

<?php
if ( isset($_GET['action']) && !empty($_GET['action']) && $_GET['action'] == 'get_time' ) {
    date_default_timezone_set('America/Vancouver');
        header("Access-Control-Allow-Origin: *");
        header('Content-type: application/json');
    echo json_encode (
        array (
              'year' => date('Y'),
                'month' => date('m'),
                'day' => date('d'),
                'hour' => date('h'),
                'minute' => date('i'),
                'second' => date('s')
            )
        );
    }
else {
    header('HTTP/1.1 404 Not Found');
    echo '<h1>404 Not Found</h1>';
    echo '<p>The page that you have requested could not be found.</p>';
}
?>

0 个答案:

没有答案