date( ) returns 1970-01-01

后端 未结 4 396
醉梦人生
醉梦人生 2021-01-14 16:37

I am using a jquery datepicker with the following format mm/dd/yyyy but I need it to be yyyy-mm-dd for the sql db so I am using this.

   $date  = date(\"Y-m-         


        
相关标签:
4条回答
  • 2021-01-14 16:58

    When you get 1970-01-01 back from date() it means that the timestamp is incorrect. That date is the UNIX epoch.

    When it comes to Javascript (in this case a datepicker) you should always make sure that what you get is, in fact, what you expect it to be. Simply passing it through strtotime() will cause problems like the one you have here.

    One good way to handle dates is using the datetime extension. It has a static method, DateTime::createFromFormat, that will let you parse any date using any format:

    // The value from the datepicker
    $dpValue = '12/30/2013';
    
    // Parse a date using a user-defined format
    $date = DateTime::createFromFormat('d/m/Y', $dpValue);
    
    // If the above returns false then the date
    // is not formatted correctly
    if ($date === false) {
        header('HTTP/1.0 400 Bad Request');
        die('Invalid date from datepicker!')
    }
    
    // Using the parsed date we can create a
    // new one with a formatting of out choosing
    $forSQL = $date->format('Y-m-d');
    
    // ...
    
    0 讨论(0)
  • 2021-01-14 16:59

    put dateformat in jquery when initializing the datepicker like

    $( "#id/.class" ).datepicker({dateFormat: 'yy-mm-dd'}); 
    

    This format will insert on mysql db(done on client side), otherwise format it on the server side code.

    0 讨论(0)
  • 2021-01-14 17:02

    $startdate is invalid and therefor the strtotime function returns anything below 86400 (most likely 0), which is the amount of seconds in one day. Please refer to the PHP documentation for the right formatting of $startdate here.

    0 讨论(0)
  • 2021-01-14 17:19

    It is called the epoch. So startdate is zero.

    0 讨论(0)
提交回复
热议问题