简体   繁体   中英

changing date format in to another in mongo db

I have a datepicker in my view page. When inserting the date into the table in this format : "Friday 20 July 2018". But I want to insert the date in normal fromat(dd-mm-YY)

view page:-

`<script>
function create_challenges(){ 
          if(! $("form#create_challenge").valid()) return false;
          if($('select[name="activity"]').val() ==0 && $('input[name="custom_activity"]').val() =="")
            { alert("Please select an activity/ add a new activity");return false;}
          $("#btn_create").prop('disabled', true);

          var formdata = $("#create_challenge").serialize()+ "&filenames="+ file_names.toString();

          $.ajax({
                url: "<?= base_url('') ?>"+"api/web/challenge/create",
                type: "POST",
                dataType : 'json', // data type
                data : formdata, // post data 
                success: function(data){

                        alert("Challenge Created Successfully");
                        redirect_url= "<?= base_url('') ?>"+"challenge/list_challenges";
                        /*var challenger_form = $('<form action="' + redirect_url + '" method="post">' +
                                      '<input type="text" name="challenge_id" value="' + data['data'] + '" />' +
                                      '</form>');
                        $('body').append(challenger_form);
                        challenger_form.submit();*/

                },
                error: function(){ $("#btn_create").prop('disabled', false); }          
            });


    }
</script>` 

in this way am posting my data in to the database.

Then how can I convert ("Friday 20 July 2018") into (dd/mm/YY") this format. enter code here

Hope this will help you :

In your controller's create method ,

First : use ci date helper's nice_date method like this :

//in autoload.php add date helper like this 
$autoload['helper'] = array('date');

 //in create method use like this 
$date = 'Friday 20 July 2018';
echo nice_date($date, 'Y-m-d').PHP_EOL;
echo nice_date($date, 'd/m/Y').PHP_EOL;

Output :

2018-07-20
20/07/2018

For more : https://www.codeigniter.com/user_guide/helpers/date_helper.html

Second : use date_create_from_format to convert date from specific format

$date = 'Friday 20 July 2018';

$mydate = date_create_from_format('l d F Y',$date);
echo date_format($mydate,'d/m/Y');
//echo date_format($mydate,'m-d-Y');

Output :

20/07/2018

Working demo : https://eval.in/1039544

For more : http://php.net/manual/en/datetime.createfromformat.php

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