最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery ui datepicker take value 1970 if input field is blank - Stack Overflow

programmeradmin0浏览0评论

i am using jquery ui date picker for take fromDate and toDate

Following is the code of from date

 <input name="from_date" id="from_date" maxlength="20" size="20" type="text"/>  

   $("#from_date").datepicker({
            dateFormat: 'M dd, yy',
            showButtonPanel: true,
            changeMonth: true,
            changeYear: true,
            defaultDate: null
        });   

now i use the form to take the value in POST varible using PHP
If the fromDate field is blank then it take value '1970-01-01' i want it blank

i am using jquery ui date picker for take fromDate and toDate

Following is the code of from date

 <input name="from_date" id="from_date" maxlength="20" size="20" type="text"/>  

   $("#from_date").datepicker({
            dateFormat: 'M dd, yy',
            showButtonPanel: true,
            changeMonth: true,
            changeYear: true,
            defaultDate: null
        });   

now i use the form to take the value in POST varible using PHP
If the fromDate field is blank then it take value '1970-01-01' i want it blank

Share Improve this question edited Nov 2, 2011 at 10:25 Reporter 3,9485 gold badges35 silver badges49 bronze badges asked Nov 2, 2011 at 10:23 Hardik RavalHardik Raval 1,9401 gold badge16 silver badges29 bronze badges 9
  • 1 Can you include the PHP script where you get the variable ? are you casting to another type in PHP ? – Manse Commented Nov 2, 2011 at 10:29
  • Following is the my PHP code $sdate = showDate($_POST['from_date'],'Y-m-d'); – Hardik Raval Commented Nov 2, 2011 at 10:32
  • and the showDate() function ? – Manse Commented Nov 2, 2011 at 10:33
  • my guess is that when using showDate or other date functions in PHP if the input is empty of null then the date that is created is epoch (ie 1970-01-01) – Manse Commented Nov 2, 2011 at 10:34
  • 1 @Matteo - that wont work if the user can select that date .... – Manse Commented Nov 2, 2011 at 10:43
 |  Show 4 more ments

3 Answers 3

Reset to default 2

Change your PHP function is use empty instead of null

function showDate($date,$format='m-d-y, h:i a') {
   if(empty($date)) return ' ';
   return date($format,strtotime($date)); 
}

try to set defaultDate: '' or remove it at all

As you can find in jquery ui documentation (here), if you don't precise defaultDate option, default date is current date.

发布评论

评论列表(0)

  1. 暂无评论