PHP mysqli从字符串中插入日期 [英] PHP mysqli insert date from string

查看:226
本文介绍了PHP mysqli从字符串中插入日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生病了所有的日期东西。
这是我尝试的:

  $ string = '24 / 10/1985'; 
$ bday = explode('/',$ string);
$ insertbday = $ bday [2]。 ' - '。 $ bday [1]。 ' - '。 $ bday [0];
$ insertbday = date(Y-m-d,$ insertbday);
mysqli_query($ con,INSERT INTO user(bday)VALUES($ insertbday));

但是,那里有0000-00-00。

解决方案

date 的第二个参数是时间戳。使用 strtotime()转换 $ insertbday



更改这一行:

  $ insertbday = date(Ymd,$ insertbday); 

to:

 code> $ insertbday = date(Ymd,strtotime($ insertbday)); 

更新



另外,在 $ insertbday 之间添加单引号来查询:

  mysqli_query($ con,INSERT INTO user(bday)VALUES('$ insertbday')); 


Im getting sick of all that date stuff. This is what I try:

$string         = '24/10/1985';
$bday           = explode('/', $string);
$insertbday     = $bday[2] . '-' . $bday[1] . '-' . $bday[0];
$insertbday     = date("Y-m-d", $insertbday);
mysqli_query($con, "INSERT INTO user (bday) VALUES ($insertbday)");

But ofc there is 0000-00-00 then. How to do this right?

解决方案

The second parameter of date is a timestamp. Use strtotime() to convert $insertbday.

Change this line:

$insertbday = date("Y-m-d", $insertbday);

to:

$insertbday = date("Y-m-d", strtotime($insertbday));

Update

Also, add single quotes around $insertbday to query:

mysqli_query($con, "INSERT INTO user (bday) VALUES ('$insertbday')");

这篇关于PHP mysqli从字符串中插入日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆