如何在PHP中跟踪我的当地时间

时间:2014-12-16 05:24:12

标签: php localtime

当我登录时以及当我退出时,我试图保持跟踪..我正在创建一个在线关注系统。我写了这些代码来追踪..但它没有在我当地的时间跟踪..当我在上午10点唱歌时它显示在下午4点签到..我该怎么办?

    $result = $_GET["result"];

    $date = date('Y-m-d');
    $day = date('l');
    $min = date('i');
    $hour = date('H');

    $min = $min - 5;
    if($min<0){ $hour=$hour-1; $min=$min+60;}
    $time = $hour.":".$min;
    $settime = $time;
    $settime1 = date('H:i');
?>
<form name="form1" method="post" action= "" >
  <table width="760" border="0" align="center" cellpadding="0" cellspacing="0">    
    <tr>
      <td colspan="3">&nbsp;</td>
      <td colspan="4" rowspan="4" valign="top"><br>    
        <table width="256" border="1" bordercolor="#CCCCCC">
            <tr> 
              <th width="82" scope="col"><div align="center">User Name </div></th>
              <th width="82" scope="col">Time Req. </th>
              <th width="78" scope="col">Time Spent </th>
            </tr>
            <?php
                $result = $_GET["result"];

                $date = date('Y-m-d');
                $day = date('l');
                $min = date('i');
                $hour = date('H');

                $min = $min - 5;
                if($min < 0){ 
                    $hour=$hour-1; $min=$min+60;
                }
                $time = $hour.":".$min;
                $settime = $time;
                $settime1 = date('H:i');

                $query = "select * from presence where uid=".$uid." and date='".$date."'";
                $recordset = mysql_query($query);
                while($record = mysql_fetch_array($recordset)){
                  $count = 1;
                  //echo "<td>Today you timed in at:- ",$record["timein"],"</td></tr><br></table>";
                  //$lasttimeout = $record["timeout"];
                }
                if($count != 1) {
                    $query2 = "insert into presence(timein, timeout, date, uid, pday) 
                    values('$settime', '$settime1', '$date', $uid, '$day')";
                    $af = mysql_query($query2);
                    //echo "<td>Today you timed in at:- ",$settime,"</td></tr><br></table>";
                    $lasttimeout = date('H:i');
                }

                //echo "<table><tr><td>Today you last timed out at:- ",$lasttimeout," 
                //</td></tr></table>";
                $array1 = array();
                $array2 = array();
                $array3 = array();
                $array4 = array();
                $userid = array();
                $great = 0; 

                function calculate($timein, $timeout) {
                    $timespentin = split(":",$timein,3);
                    $timespentout = split(":",$timeout,3);
                    //print_r($timespentin);
                    //print_r($timespentout);
                    $t1 = $timespentin[0]*60 + $timespentin[1];
                    $t2 = $timespentout[0]*60 + $timespentout[1];
                    $timespent = round(($t2 - $t1)/60,2);
                    //echo $timespent;
                    return $timespent;
                 }

5 个答案:

答案 0 :(得分:0)

使用date_default_timezone_set函数

 date_default_timezone_set("Asia/Calcutta");

 $login_time = date("Y-m-d H:i:s");

答案 1 :(得分:0)

PHP安装在服务器和默认UTC时区,您需要根据当地时区更改时区,将date_default_timezone_set("time_zone");功能置于脚本顶部

<?php
date_default_timezone_set("Asia/Bangkok");
echo date_default_timezone_get();
echo date('Y-m-d H:i:s')
?> 

TIMEZONE list

答案 2 :(得分:0)

你在哪个时区?你应该使用

设置默认时区
date_default_timezone_set('Asia/Kolkata');

在使用date函数之前,此行应位于文件的顶部。 详情阅读setting default timezonetimezone regions

答案 3 :(得分:0)

你可以这样做:

date_default_timezone_set('Asia/Kolkata');//set your time zone 

echo date('Y-m-d H:i:s');// get the date

在此处查看设置默认时区:http://php.net/manual/en/function.date-default-timezone-set.php 和时区:http://php.net/manual/en/timezones.php

答案 4 :(得分:0)

DateTime::getTimestamp()返回unix时间戳。该数字始终为UTC。你想要的是根据你的时区格式化日期。

$dt = new DateTime("now", new DateTimeZone('Asia/Kolkata'));

echo $dt->format('m/d/Y, H:i:s');

或根据您的需要使用不同的日期格式。

此外,您可以使用DateTime库来满足您的所有日期需求。每次要获取日期时都无需更改服务器的默认时区。

阅读here了解详情

来源:https://stackoverflow.com/a/24914436/1877909