简体   繁体   中英

bootstrap datetimepicker dateformat saving to mysql

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)');

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM