Codeigniter - human_to_unix正确的输入格式?

时间:2015-12-03 09:32:12

标签: php codeigniter

我有一个表单,其中包含一个日期字段。

日期将在format dd-mm-yyyy (e.g. 08-11-2015)

中收到

我希望在插入数据库之前将其转换为unix时间戳。

如果可能,我想使用Codeigniter函数而不是本机PHP函数。

Codeigniter具有以下功能:

human_to_unix($input)

该函数不接受格式字符串,我找不到正确的格式(现在08-11-2015 is 8th March 2015而不是11th August 2015 ??)。

由于

3 个答案:

答案 0 :(得分:0)

你必须使用这样的日期格式来获取

 human_to_unix -> YY-MM-DD HH:MM <space> meridian

<强>例如:

$this->load->helper('date_helper'); 
human_to_unix("2015-12-03 03:28 PM");

<强>输出:

  

1449136680

答案 1 :(得分:0)

$this->load->helper('date');
$input = strtotime('08-11-2015');

$human = unix_to_human($input);
$unix = human_to_unix($human);

输出将是: 2015-11-08 12:00 AM

答案 2 :(得分:0)

CodeIgniter中的默认date_helper无法接受dd-mm-yyyy格式的日期。您可以使用标准PHP(尽管您指定了您更喜欢CodeIgniter函数):

$date = DateTime::createFromFormat('d-m-Y', '08-11-2015');
echo $date->format('U');

或者,您可以{Cn}提供extend the helpers

创建新文件application/helpers/MY_date_helper.php

<?php
function human_to_unix_alternative($datestr) { // change this to a suitable function name
    $date = DateTime::createFromFormat('d-m-Y', $datestr);
    return $date->format('U');
}

然后,您可以在代码中使用它,就像您使用human_to_unix($input)一样,例如human_to_unix_alternative($input)