在我的php代码中,我在我的数据库中插入一些数据。一个是约会。我希望以27-08-2016的格式显示,但我回到01-01-1970
这是我的代码。
<?php
include("./init.php");
if(isset($_POST['submit'])){
$post_game = $_POST['game'];
$time = strtotime($_POST['date']);
$post_date = date("d-m-Y", strtotime($time));
if($post_game==''){
echo "<script>alert('Please fill in all fields')</script>";
exit();
}
else {
$insert_game = "insert into last_game (game,date) values ('$post_game','$post_date')";
$run_posts = mysqli_query($con,$insert_game);
echo "<script>alert('Post Has been Published!')</script>";
echo "<script>window.open('index.php?last_game_details','_self')
</script>";
}
}
?>
关键路线是:
$post_date = date("d-m-Y", strtotime($time));
我的php版本是: 5.6.23
答案 0 :(得分:2)
slabService
答案 1 :(得分:1)
请在查询中使用Now()来插入当前日期。 NOW()返回当前日期和时间。
if(isset($_POST['submit'])){
$post_game = $_POST['game'];
$time = strtotime($_POST['date']);
$post_date = date("d-m-Y", strtotime($time));
if($post_game==''){
echo "<script>alert('Please fill in all fields')</script>";
exit();
}
else {
$insert_game = "insert into last_game (game,date) values ('$post_game',NOW())";
$run_posts = mysqli_query($con,$insert_game);
echo "<script>alert('Post Has been Published!')</script>";
echo "<script>window.open('index.php?last_game_details','_self')
</script>";
}
}
?>
要检索所需的日期格式,请使用php
`echo date_format($date,"Y-m-d ");`
答案 2 :(得分:0)
你的问题在这里:
$time = strtotime($_POST['date']);
$post_date = date("d-m-Y", strtotime($time)); //why call strtotime again?
第二次拨打strtotime
时,您正在为其添加时间戳(第一次通话的结果),但它需要一个日期字符串。因此,它无法解析输入并返回无效时间。只需将以上两行替换为:
$time = strtotime($_POST['date']);
$post_date = date("d-m-Y", $time);