strtotime()在格式化日期会抛出false [英] strtotime() throws false on a formatted date

查看:121
本文介绍了strtotime()在格式化日期会抛出false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将格式化的日期字符串转换为整数时间戳记时遇到问题。我正在使用datepicker输入一个字段,例如 DD / MM / YY ,我使用 strtotime 更改它。

I'm having a problem with converting a formatted date string into a integer timestamp. I'm using a datepicker to input a field such as DD/MM/YY and I'm using strtotime to change it.

示例用法

$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"

现在你可以看到每个变量都不是空的, code> false )当使用 strtotime 与此:

Now you can see each variable are not empty, now nothing is returning (except false) when using strtotime with this:

$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 转换成时间戳,我几乎没有找到它的错误。

Now that's where I'm stuck thinking why it's not converting a DD/MM/YY HH:MM into a timestamp, I hardly can find what's wrong with it...

推荐答案

请参阅 http: //php.net/strtotime


m / d / y或dmy格式的日期
被消除歧义通过查看各种
组件之间的
分隔符:如果分隔符是
斜线(/),则美式m / d / y假定为
;而如果分隔符是
破折号( - )或点(。),则假定
欧洲dmy格式。

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.


$ m / d / y中的b $ b

23/03/2011无效,因此函数返回false。

23/03/2011 in m/d/y is invalid, as such the function returns false.

这篇关于strtotime()在格式化日期会抛出false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆