我在将格式化日期字符串转换为整数时间戳时遇到问题。我使用日期选择器输入诸如 DD/MM/YY
之类的字段,并使用 strtotime
来更改它。
使用示例
$date_from = (!empty($_POST['datefrom'])) ? (string) $db->sql_escape($_POST['datefrom']) : false;
$date_to = (!empty($_POST['dateto'])) ? (string) $db->sql_escape($_POST['dateto']) : false;
$time_from = (!empty($_POST['timefrom'])) ? (string) $db->sql_escape($_POST['timefrom']) : '';
$time_to = (!empty($_POST['timeto'])) ? (string) $db->sql_escape($_POST['timeto']) : '';
转储
var_dump($_POST);
var_dump($date_from . ' ' . $time_from);
var_dump($date_to . ' ' . $time_to);
输出
array(4) {
["datefrom"]=>
string(10) "23/03/2011"
["dateto"]=>
string(10) "18/04/2011"
["timefrom"]=>
string(5) "01:26"
["timeto"]=>
string(5) "04:44"
}
string(16) "23/03/2011 01:26"
string(16) "18/04/2011 04:44"
现在您可以看到每个变量都不为空,当使用 strtotime
时,现在没有任何返回(除了 false
):
$range_from = strtotime($date_from . ' ' . $time_from);
$range_to = strtotime($date_to . ' ' . $time_to);
var_dump($range_from);
var_dump($range_to);
输出
bool(false)
bool(false)
现在我一直在思考为什么它不将 DD/MM/YY HH:MM
转换为时间戳,我几乎找不到它有什么问题......
最佳答案
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.
23/03/2011 在 m/d/y 中无效,因此该函数返回 false。
关于php - strtotime() 在格式化日期上抛出 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5736205/