duanjuhuo8772 2011-04-20 20:25
浏览 213
已采纳

strtotime()在格式化日期抛出false

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.

Example usage

$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']) : '';

Dumps

var_dump($_POST); 
var_dump($date_from . ' ' . $time_from);
var_dump($date_to . ' ' . $time_to);

Outputs

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"

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);

Outputs

bool(false)
bool(false)

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...

  • 写回答

1条回答 默认 最新

  • dongpinyao2203 2011-04-20 20:30
    关注

    See http://php.net/strtotime

    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 in m/d/y is invalid, as such the function returns false.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题