doukuo9116 2013-09-21 08:34 采纳率: 100%
浏览 27
已采纳

如何使用PHP获取用户的pc时间

I am working on an application in which I need to get date & time of local pc of user. Basically php have date and time functions and it returns server time as it is server side scripting language.

I am getting correct time using javascript Date function but when i use strtotime function in php it doesn't work, i need a way to get local time in php.

<script type="text/javascript">

function getDate() 
{ 
        var d = new Date();
    var c_day = d.getDate();
    var c_month = d.getMonth() + 1;
    var c_year = d.getFullYear();
    var c_hour = d.getHours(); 
    var c_min = d.getMinutes(); 
    var t = c_day+"-"+c_month+"-"+c_year+" "+c_hour+":"+c_min;
    return t;
}
if (window.XMLHttpRequest)
{
    xmlhttp=new XMLHttpRequest();
}
else
{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    { 
        alert(xmlhttp.responseText);        
    }
}    
xmlhttp.open("GET","setdate.php?sdate="+encodeURIComponent(getDate()),false);
xmlhttp.send();
</script>

The file 'setdate.php' stores sdate value in the session and i am getting it as an output of ajax.

<?php
$sdate = $_SESSION['sdate'];
$date = date( "d/m/Y", strtotime( $sdate ) );
?>

This works after refreshing the page, but not when the page loads first time.

Any one can help me on this ?

  • 写回答

5条回答 默认 最新

  • dounai6626 2013-09-21 08:45
    关注

    You can not get Client Side Date using php alone.

    For client side, you would need Javascript, something like the following should do the trick.

    var currentTime = new Date();
    

    You can use AJAX to send this to server.

    var currentTime = new Date();
    $.ajax({
        url : url,
        data : {date : currentTime}
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题