duankui6150 2013-10-27 06:37
浏览 36
已采纳

jquery datepicker在php上转换为date()

i have a script that saves a date like this.

$date = date('i')+50;

When i store this on my database, i got something like 1382851302 wich works great to compare for past and future just using > or <.

In other part of the script for the admin im using a filter based on jquery datepicker.. the problem is i cant convert the date selected there to the same "date()" format so i can compare the date , i think time is somehow envolved here (i) and well i cant.

here is my bad try.

$iniciopub = date('Y-m-d',strtotime($_POST['iniciopub']));

$finpub = date('Y-m-d',strtotime($_POST['finpub']));

This is not working.. (it just store "1970" )

if i try this..

  $iniciopub = date($_POST['iniciopub']);

    $finpub = date($_POST['finpub']);

I just save the day , i think because the format is like 00/00/0000 so the / is "cutting" the value cause i can just save int. in the database as you can see in the format i need, i just nned numbers..

Really lost on this, sorry if this is a fool question.

Im studing so dont be so hard. EDIT:

Ok this is my code now im using mktime()

$iniciodate = str_replace("/",",",$_POST['iniciopub']);
           $iniciopub = mktime(0,0,0,$iniciodate);

           $findate = str_replace("/",",",$_POST['finpub']);
           $finpub = mktime(0,0,0,$findate);

This saves this to timestamps for 10/27/2013 = 1382824800 but the same for 10/31/2013

ALso im using this format, i have not problem with it

dateFormat: 'mm/dd/yy',

This is now set in datepicker jquery ui

  • 写回答

2条回答 默认 最新

  • douzen1880 2013-10-27 07:33
    关注

    Your variable $iniciopub has value 1970-01-01 because strtotime() returned false:

    var_dump( strtotime($_POST['iniciopub']) );
    

    Take a look at supported date and time formats. If you are saying that you have format like 00/00/0000 in $_POST, then strtotime asumes this is american month, day and year (mm/dd/yyyy), like it states here.

    If inputed format 00/00/0000 is dd/mm/yyyy then the easiest method would be to use DateTime::createFromFormat or str_replace('/', '-', 'dd/mm/yyyy'); to make it european date format dd-mm-yyyy.

    Question update

    This code is just wrong:

    $iniciodate = str_replace("/",",",$_POST['iniciopub']);
    $iniciopub = mktime(0,0,0,$iniciodate);
    

    mktime() has 6 parameters (7th is is_dst wthich doesn't matter now). You cannot use $iniciodate like that, because you are inputing only 4th parameter into mktime() function, and not 4th, 5th and 6th as you might think.

    mktime(0,0,0,'10,27,2013'); is not the same as mktime(0,0,0,10,27,2013); or `mktime(0,0,0,'10','27','2013');. The reason why date 10/27/2013 and 10/31/2013 return same timestamp is because in both cases, when you cast string 10,27,2013 and 10,31,2013 to integer (4th parameter), you get 10. See:

    $v = '10,27,2013'; var_dump( (int)$v ); # int(10)
    $v = '10,31,2013'; var_dump( (int)$v ); # int(10)
    

    And because of that, your call is the same if you would call mktime() like:

    var_dump(  mktime(0,0,0,10) ); # int(1382832000)
    

    If you would have error_reporting on (put error_reporting(-1); on the beggining of the php file), you would see, that after calling mktime(0,0,0,$iniciodate) you would get NOTICE:

    Notice: A non well formed numeric value encountered in file.php on line XX

    Since 10/27/2013 and 10/31/2013 are standard american date formats, you can just use strtotime() instead of mktime() to convert date to timestamps:

    var_dump( strtotime('10/27/2013') ); # int(1382832000)
    var_dump( strtotime('10/31/2013') ); # int(1383177600)
    

    You asked in the comments: if i store a date as dd-mm-yyyy. how can i compare the date with other date. am i doing fine storing as timestamp?

    Yes, you can store them as timestamp. But I haven't used timestamp in my code for ages, I am storing dates as YYYY-MM-DD; in RDBMS this is kinda best practice.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用