我想解析日期字符串,而不必操纵字符串,并期望给定的格式。PHP日期 - 字符串到时间戳
我希望我的用户能够输入January 2, 2011
或1-2-11
,并且仍然最终以2011-01-02 12:00:00
保存在数据库中。
目前,我有这样的到位:
$post_date = date("Y-m-d H:i:s", strtotime(stripslashes($_POST['post_date'])));
但似乎是的strtotime返回0,因为作为日期时间1969-12-31 17:00:00
我需要做什么改变结束了?
UPDATE
从php.net:
(PHP 4,PHP 5)
的strtotime - 解析任何英文文本的日期时间描述为Unix时间戳。
..我猜不是!
我不是想的strtotime(数组:
if(isset($_POST['post_date'])){
foreach($_POST['post_date'] as $id => $post_date){
print $post_date.'<br />';
if(strlen(trim($post_date)) && $post_date !== 'Date posted'){
$update_data[$id]['post_date'] = date("Y-m-d H:i:s", strtotime(stripslashes($post_date)));
}
}
}
得到它的工作:
if(isset($_POST['post_date'])){
foreach($_POST['post_date'] as $id => $post_date){
print $post_date.'<br />';
if(strlen(trim($post_date)) && $post_date !== 'Date posted'){
$post_date = str_replace(',', '', $post_date);
$post_date = str_replace('-', '/', $post_date);
$update_data[$id]['post_date'] = date("Y-m-d H:i:s", strtotime(stripslashes($post_date)));
}
}
}
由于多个供我删除逗号和替换连字符。正斜杠
什么是$ _POST ['post_date']目前? – genesis
$ _POST ['post_date']实际上是一个数组(更新多个记录),但示例值将是'2012年12月31日' – AVProgrammer
同意创世纪。我只是用PHP5.3测试它,它运行良好。但要记住的一点是,时间通常会将# - # - #读作Year-Month-Date。如果你想要不同的行为,你可能需要先解析它。 – Dan