0

i have tried several ways to convert date selected with datetimepicker to save it in my mysql database but none of them works.

i want to save this format:

  10 Octobre 2015 - 12:28 pm

but i have this saved:

 01 Jan 1970

any suggestions? Many tks

My code to select date:

<div class="input-group date form_datetime col-md-6"  data-date-format="dd MM yyyy - HH:ii p" data-link-field="dtp_input1">
   <input class="form-control" name="date_from" size="16" type="text" value="" readonly>
</div>

and for saving it:

$date_from=$_POST['date_from'];
$query = $db->prepare('INSERT INTO events (date_from, title) 
    VALUES (:date_from, :titre)');
2
  • Provide your SQL. Either you're using a unix timestamp (integer) or a TIMESTAMP field that is being evaluated as 0 (the start of unix time). dev.mysql.com/doc/refman/5.5/en/datetime.html The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC. Commented Oct 15, 2015 at 22:42
  • Datetime should be in a YYYY-MM-DD HH:MM:SS format for SQL. Commented Oct 15, 2015 at 22:57

3 Answers 3

1

A little late to the party but you can use the following to grab input on submit. This was designed for converting multiple inputs but can be edited to run on just one as well if you so desired.

The following allows you to display your time selections in 12 hour format, as well as have datetimepicker js apply to any dynamically created elements.

$(document).on("mouseover mouseout", ".datetimepicker", function(){
    $(this).datetimepicker({
        format:'YYYY-MM-DD hh:mm:00 a'
    })
});

btw, datetimepicker requires moment.js so before you submit run this to save to mysql.

$('form').submit( function () {
    $('.datetime').each( function() {
        $(this).val(moment(new Date($(this).val())).format("YYYY-MM-DD HH:mm:ss"))
    });
})

To display the time as you had mentioned you may need to apply the styling to the date when you're rendering it.

Sign up to request clarification or add additional context in comments.

Comments

0

Setup your MySQL field as a DATETIME column, then do this to your datetimepicker:

$('#input').datetimepicker(
  'dateFormat': "yy-mm-dd",
  'timeFormat': "HH:mm:ss"
);

Comments

0

There are some things to know about Bootstrap datepicker.

First it isn't made to handle form submit event so when you set your data-date-format="dd MM yyyy - HH:ii p", you are getting exactly that POST string. That means a preprocess are needed.

You can do so by the client side or sever side. Always is better work in client side so save resources of our server. You can build also a function inside your PHP to convert to MySQL format your POST string.

Remember if you process at client side you need to send the POST in MySql Format(YYYY-mm-dd for DATE) or (YYYY-mm-dd H:i:s for DATETIME or TIMESTAMP)

Ex.: // Format your datetimepicker with Mysql format

$("input[name='date_from']").datepicker().on('changeDate', function(e){
 var dateFrom = new Date(e.date.getFullYear(), e.date.getMonth(), e.date.getDate(), e.date.getHours(),e.date.getMinutes(),0);
});

Now you can push this value on a hidden field: HTML:

<input id="sqldate_from" type="hidden" name="sqldate_from" />

SCRIPT:

$("input[name='date_from']").datepicker().on('changeDate', function(e){
     var dateFrom = new Date(e.date.getFullYear(), e.date.getMonth(), e.date.getDate(), e.date.getHours(),e.date.getMinutes(),0);
    });
    $("#sqldate_from").val(dateFrom);

PHP:

$date_from = $_POST['sqldate_from'];

You also can get the original string without treatment and process in PHP...something like this (only for YYYY-mm-dd format):

public function formatDatepickerToMySql($date) {
    if ($date != FALSE) {
        $dateArr = explode("-", $date);
        $newDate = $dateArr[2] . '-' . $dateArr[1] . '-' . $dateArr[0];
        return $newDate;
    }
    return FALSE;
}

Hope be usefull

KR apassos

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.