如何在php中设置时间

时间:2014-03-14 09:22:00

标签: php mysql

这是我的PHP代码。

我想添加添加日期的时间,但我需要知道如何在此脚本中添加时间。

<link rel="stylesheet" type="text/css" href="tcal.css" />
<script type="text/javascript" src="tcal.js"></script>
<form action="index.php" method="get">
From : <input type="text" name="d1" class="tcal" value="" /> 
<input type="submit" value="Search">
</form>
<table id="resultTable" data-responsive="table" style="text-align: left; width: 400px;" border="1" cellspacing="0" cellpadding="4">
<thead>
  <tr>
     <th> Birtday </th>
     <th> Name </th>
     <th> Gender </th>
  </tr>
</thead>
<tbody>
  <?php
    include('connect.php');
    if (isset($_GET["d1"])) { $d1  = $_GET["d1"]; } else { $d1="0000-00-00"; }; 
    $result = $db->prepare("SELECT * FROM birthday WHERE date = :a");
    $result->bindParam(':a', $d1);
    $result->execute();
    for($i=0; $row = $result->fetch(); $i++){
  ?>
<tr class="record">
    <td><?php echo $row['date']; ?></td>
    <td><?php echo $row['name']; ?></td>
    <td><?php echo $row['gender']; ?></td>
</tr>
<?php
} 
?>
</tbody>
</table>

以下是我想添加时间的代码?

if (isset($_GET["d1"])) { $d1  = $_GET["d1"]; } else { $d1=date('Y-m-d')$d1=time('H:i:s'); };

我可以知道在脚本中添加时间应该做些什么改变吗?

2 个答案:

答案 0 :(得分:0)

if (isset($_GET["d1"])) { $d1  = $_GET["d1"]; } else { $d1=date('Y-m-d H:i:s'); };

答案 1 :(得分:0)

if (isset($_GET["d1"]))
{ 
     $d1  = $_GET["d1"]; 
} 
else 
{ 
     $d1=date("Y-m-d H:i:s");
}

有关php参考的更多信息:http://www.php.net/manual/en/function.date.php

根据您的评论更新问题:

这里让我们举一个输入字段的例子,以及如何在其中显示日期的值。你的代码将是这样的:

<input type="date" value="<?php echo date("Y-m-d H:i:s"); ?>" />

SQL查询评论的更新2:

首先在用于查询数据库的php变量中进行sql查询:

$current_date = date("Y-m-d H:i:s");
$query = "SELECT * FROM birthday  where date <= $current_date";

有关mysql处理的更多信息here是一个很棒的教程,this one也是如此: