当我将它插入到我的数据库中时,它工作了:
$time = strtotime('1' . '/' . 10 . '/' . '2015');
$res = date('y-n-d',$time);
但是当我把它插入到我的数据库时,它没有:
$time = strtotime('10' . '/' . 10 . '/' . '2015');
$res = date('y-n-d',$time);
这段代码在我的数据库中插入了1970-01-01的默认日期…
唯一的变化是一天的长度。我试着改变日期的格式,比如Y-m-d,但是它没有改变任何东西。
使用DateTime
接口-
$time = new DateTime('10' . '/' . 10 . '/' . '2015');
echo $res = $time->format('Y-m-d');
按以下方式使用…
$time = date("d-m-Y", strtotime('1/10/2015'));
$res = date("Y-m-d", $time);
或
$date = date_create('1/10/2015');
$res = date_format($date, 'Y-m-d');
看起来您试图在DATE字段中插入时间戳(整数)。如果你的数据库是MySQL并且你使用的是DATETIME类型,那么你可以使用这个php日期格式:
date("Y-m-d H:i:s")
另外,我建议使用@sgtBose提到的new DateTime(),因为您可能会从不同地区的strtotime()中获得意想不到的结果。
试试这个:
$unixDate='01/01/1900';$differenceValue = strtotime($time)- strtotime($unixDate);