【问题标题】:PHP timestamp convert to javascript?PHP时间戳转换为javascript?
【发布时间】:2014-02-13 15:35:50
【问题描述】:

朋友们好,这是我的 php 代码。

<?php
   $date3=date_default_timezone_set('Europe/Paris');
   echo $abc3=date('Y-m-d h:i:s');
   $t3= strtotime($abc3) * 1000;
?>

打印日期为2014-01-23 08:43:45

朋友们,这是我的 Javascript 代码

<script>
 var t3=<?php echo $t3; ?>;
function update_clock3(){

var now = new Date(Number(t3));

var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();   

alert(hours +":"+ minutes + ":" + seconds); 

}
</script>

Javascript 代码是打印日期错误。

javascript 输出下。

13:13:45

请帮忙解决我的错误。

谢谢。

【问题讨论】:

    标签: javascript php timestamp


    【解决方案1】:

    在下面使用:-

    <script>
    var t3 = '<?php echo $t3; ?>';
    
    function update_clock3(){
    
        var now = new Date(t3);
    
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds();   
    
        alert(hours +":"+ minutes + ":" + seconds); 
    
    }
    </script>
    

    编辑:-

    <?php
       $date3=date_default_timezone_set('Europe/Paris');
       echo $abc3=date('Y-m-d h:i:s');
       $t3= strtotime($abc3) * 1000;
    ?>
    
    <script>
    var t3 = '<?php echo $t3; ?>';
    alert(t3);
    
    </script>
    

    【讨论】:

    • @renishkhunt 你想要 2014-01-23 08:43:45 的 JavaScript 输出吗?
    • @renishkhunt 然后检查我编辑的代码。你为什么要使用 Date() 函数?试试上面。它会发出类似 1390463898000 的警报。
    【解决方案2】:

    直接在日期函数内部传递日期字符串

    http://www.w3schools.com/jsref/jsref_obj_date.asp
    <!DOCTYPE html>
    <html>
    <body>
    
    <p id="demo">Click the button to display the hour of the time right now.</p>
    
    <button onclick=" update_clock3()">Try it</button>
    
    <script>
    
     var t3='2014-01-23 08:43:45';
    function update_clock3(){
    
    var now = new Date(t3);
    
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds();   
    
    alert(hours +":"+ minutes + ":" + seconds); 
    
    }
    
    </script>
    
    </body>
    </html>
    

    【讨论】:

    • in t3 is not a string date is a timestamp like 1390463898000
    • "打印日期是 2014-01-23 08:43:45" 为什么你这样提到。上面的答案适用于这个日期字符串
    【解决方案3】:

    您只需要将客户端时间转换为服务器时区。使用Date.prototype.getUTCHours 等方法。您也可以使用Date.prototype.getTimezoneOffset() 来检查时区差异并在日期更改时通知使用,例如:

    <script>
    
        var t3=<?php echo $t3; ?>;
        function update_clock3(){
    
            var now = new Date(Number(t3));
            var year = now.getUTCFullYear();
            var month = now.getUTCMonth();
            var day = now.getUTCDate();
            var hours = now.getUTCHours();
            var minutes = now.getUTCMinutes();
            var seconds = now.getUTCSeconds();  
            //local zone - UTC zone, so +1 UTC will be -60
            var offset = now.getTimezoneOffset()/60;
            //convert hours from UTC time zone to local
            var minutesOffset = offset % 1;
            var localHours = hours - Math.floor(offset);
            var localMinutes = minutes - minutesOffset * 60; 
            var localDate = new Date(year,month, day, localHours, localMinutes, seconds);
    
            //day can be change by adding offset
            if (localDate.getDate() !== day) {
                alert("You have another day");
            }
    
            alert("UTC time:" + hours +":"+ minutes + ":" + seconds); 
    
        }
    </script>
    

    【讨论】:

    • 嘿,pinal,请您在这个时区使用。 'Asia/Hong_Kong'
    • 我在这个时区上的时间不同。
    • 我编辑我的答案,添加时区检查并删除 Europe/Paris 硬代码。
    猜你喜欢
    • 2015-09-08
    • 2016-04-03
    • 2011-07-21
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多