0

可能重复:
如何格式化从数据库接收到的日期和时间

代码的输出

我想只在“In Comming Date”列中显示日期,并且只在“In Comming Time”中显示时间我的代码是任何人都可以帮助我,我是新手

$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

4 回答 4

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>
于 2012-11-20T12:18:24.067 回答
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'";
于 2012-11-20T12:24:00.960 回答
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您喜欢的日期/时间格式。

于 2012-11-20T12:17:54.813 回答
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"]));
于 2012-11-20T12:33:54.463 回答