Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting string to datetime from jQueryUI datepicker using strtotime

I am using jQueryUI datepicker and here is the code.

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#from" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
      onClose: function( selectedDate ) {
        $( "#to" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "#to" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
      onClose: function( selectedDate ) {
        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
  });
  </script>

    <form action="{LINKTO_EXPORT}" method="post">
        <h1>{EXPORT_CSV}</h1>
        <label for="from">from</label>
        <input type="text" id="from" name="from">
        <label for="to">to</label>
        <input type="text" id="to" name="to">       
        <input type="submit" value="{EXPORT_TEXT}">
    </form>

When I "post" the "from" and "to",

$fromstr = $_POST["from"];
$tostr = $_POST["to"];
$from =  date('Y-m-d H:i:s',strtotime($fromstr." 02:00:00"));
$to =  date('Y-m-d H:i:s',strtotime($tostr." 02:00:00"));

the to has been converted properly, i.e., 2015-06-13 02:00:00 but the from has not. It returned 6/1/2015 2:00 instead. To make sure I am fetching the correct values, I echoed the $fromstr and $tostr.

$fromstr returned 6/1/2015 $tostr returned 06/13/2015

Why was the from returned m/d/yyyy while to did mm/dd/yyyy? How do I convert a m/d/yyyy string to timestamp then? Please help, thank you!

like image 605
user4668176 Avatar asked May 27 '15 03:05

user4668176


1 Answers

Try this..

Download following js

<link rel="stylesheet" type="text/css" href="http://xdsoft.net/scripts/jquery.datetimepicker.css"/>
<script src="http://xdsoft.net/scripts/jquery.datetimepicker.js"></script>

You cant use directly ,so download js and css.

  <script>
  $(function() {

$('#datetimepicker8').datetimepicker({
    onGenerate:function( ct ){
        $(this).find('.xdsoft_date')
            .toggleClass('xdsoft_disabled');
    },
    minDate:'-1970/01/2',
    maxDate:'+1970/01/2',
    timepicker:false
});
$('#datetimepicker9').datetimepicker({
    onGenerate:function( ct ){
        $(this).find('.xdsoft_date')
            .toggleClass('xdsoft_disabled');
    },
    minDate:'-1970/01/2',
    maxDate:'+1970/01/2',
    timepicker:false
});
  });
  </script>

<input type="text" id="datetimepicker8"/>
<input type="text" id="datetimepicker9"/>
like image 152
Deenadhayalan Manoharan Avatar answered Oct 04 '22 11:10

Deenadhayalan Manoharan