如何在时间戳字段中设置日期和时间格式

时间:2012-11-20 12:14:53

标签: php mysql date timestamp

  

可能重复:
  How do I format the date and time That is received from database

Output of the code

我想只在“参与日期”列中显示日期,并且只在“在时间内”显示时间 我的代码是这可以任何人帮助我,我是一个新手

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
$sql="SELECT * FROM `hr_inout_time` WHERE user_id='$login_id'";
$result = mysql_query($sql) or die(mysql_error());
echo "<table align='center' border='1' cellspacing='5'  width='50%' height='50%'>
<th>In Comming Date</th> 
<th>In Comming Time</th> 
";
while($row=mysql_fetch_array($result)) {

    $in_time=$row["user_in_time"];
    $time_date =$row["time_date"];
    echo "<tr>
            <td align='center'>$time_date</td> 
             <td align='center'>$in_time</td> 
         </tr>";
    }
echo('</table>');
?>

4 个答案:

答案 0 :(得分:1)

简短回答 - 替换:

<td align='center'>$time_date</td> 
<td align='center'>$in_time</td>

使用

<td align='center'>".explode(" ",$time_date)[0]."</td> 
<td align='center'>".explode(" ",$in_time)[1]."</td>

答案 1 :(得分:0)

更改

$in_time=$row["user_in_time"];
$time_date =$row["time_date"];

$in_time = date('H:i', strtotime($row["user_in_time"]));
$time_date = date('d F Y', strtotime($row["time_date"]));

H:id F Y更改为您首选的日期/时间格式。

答案 2 :(得分:0)

您可以使用SQL完成所有操作,因此您无需在PHP脚本中进行转换。由于您的日期看起来存储为DATETIME值,您可以使用DATE()和TIME()函数返回值的那些部分

$sql = "SELECT hr_inout_time.*, 
  DATE(hr_inout_time.time_date) as time_date, 
  TIME(hr_inout_time.user_in_time) as user_in_time 
FROM `hr_inout_time` 
WHERE user_id='$login_id'";

答案 3 :(得分:0)

$in_time = $row["user_in_time"];

$time_date = $row["time_date"];

为:

$in_time = date('H:i:s', strtotime($row["user_in_time"]));

$time_date = date('Y-m-d', strtotime($row["time_date"]));