我试图从mysql的datetime向表单添加一个默认值。这段代码可以工作,但我想显示下一个日期,即比从数据库中获取的日期少一天。我试了很长时间,但没有结果。。如何从日期时间中减去一天?

<input type="text" name="pub" value="
<?php
 $con=mysql_connect("localhost","user","pass") or die("Failed to connect to MySQL: " . mysql_error());
 $db=mysql_select_db("kalender",$con) or die("Failed to connect to MySQL: " . mysql_error());
 $result = mysql_query("SELECT DISTINCT pub FROM event ORDER BY `id` DESC LIMIT 1");
 $data = mysql_fetch_array($result);

 echo $data['pub'];
 ?>
">
</input>

最佳答案

你的代码很糟糕:我修改了检查:

<?php
 $con=mysql_connect("localhost","user","pass") or die("Failed to connect to MySQL: " . mysql_error());
 $db=mysql_select_db("kalender",$con) or die("Failed to connect to MySQL: " . mysql_error());
 $result = mysql_query("SELECT DISTINCT pub FROM event ORDER BY `id` DESC LIMIT 1");
 $data = mysql_fetch_array($result);

 $dates = $data['pub'];
 $date = date("Y-m-d",strtotime($data['pub'].' -1 day'));
 ?>
<input type="text" name="pub" value="<?php echo $date;?>" />

这将为您提供所选日期的上一个日期。

关于php - 如何从php/mysql的datetime中减去day?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20771203/

10-12 14:17