将唯一字符串转换为时间戳

时间:2017-09-22 05:28:49

标签: php html mysql

我想问。

我从html中获取数据时间,并尝试插入我的数据库。 插入格式如“ 8:16:25 PM 8/8 / 2017 ”。 我如何转换为时间戳格式,因为我想要范围它。 我尝试剪切字符串,并在插入之前使其成为utf8_decode或utf8_encode。但是当范围我无法得到时间。对不起我的英文。

代码:

$first = array('PM','/','AM');
$first2 = array('','-','');
$strreplacebegdate = str_replace($first,$first2, $tujuh);
$substrreplacebegdate1 = substr($strreplacebegdate,14,9);
$re = array(' ','?');
$re2 = array('','');
$strreplacebegdatelagi = str_replace($re,$re2, $substrreplacebegdate1);
$substrreplacebegdate2 = substr($strreplacebegdate,6,7);
$consubstringbegdate = $substrreplacebegdate1." ".$substrreplacebegdate2;
$timeenddate = strtotime($strreplacebegdatelagi);
$newformatbegdate = date('Y-m-d',$timeenddate);

输出:0000-00-00 00:00:00

2 个答案:

答案 0 :(得分:1)

它更喜欢你清理你的日期字符串。在这种情况下,您可以使用正则表达式,只允许特定字符传递并删除所有其他特殊字符和隐藏字符。

$dirty_date = 'Â Â 8:16:25 PM 8/8/2017Â Â';
echo $date = preg_replace("/[^0-9a-zA-Z \/:\-]/", "", $dirty_date);
echo "\n";
echo $xtime = strtotime($date);
echo "\n";
echo date("Y-m-d H:i:s",$xtime);

答案 1 :(得分:0)

您无法使用strtotime()。由于您的日期格式不同。您可以这样做。

$str_time = 'Â Â 8:16:25 PM 8/8/2017Â Â';
$date = DateTime::createFromFormat('Â Â h:i:s a d/m/YÂ Â', $str_time);
$timestamp = $date->getTimestamp();

阅读有关createFromFormat()的更多信息。访问此http://php.net/manual/en/datetime.createfromformat.php。我希望这有帮助

相关问题