Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to save the exact date in database

I have tried many times to store the exact date which I have selected. But when saving, it will take the previous date. I have tested in many ways, the angular file will pass the same selected date. When the sql query is executed, then it will take the incorrect date. can anyone please help me to solve this?

Below is my code. html code:

     <td class="data_field">
         <input class="form-control" type="date" name="date_main_domain" ng-model="domain.date_main_domain" id="date_main_domain" required value="{{domain.date_main_domain}}">
     </td>

sql query:

    $database->execute( "UPDATE domain_information SET date_main_domain='$dateMainDomain' WHERE id=$domainId" );
like image 663
Bhavya Hegde Avatar asked Nov 20 '25 19:11

Bhavya Hegde


1 Answers

You need a valid date for the database to accept it, format your date object

        function formatDate(date) {
            var d = new Date(date),
                month = '' + (d.getMonth() + 1),
                day = '' + d.getDate(),
                year = d.getFullYear();

            if (month.length < 2) month = '0' + month;
            if (day.length < 2) day = '0' + day;

            return [year, month, day].join('-');
        }

        // date object to save
        var date = new Date(formatDate($scope.domain.date_main_domain));
like image 117
cute_programmer Avatar answered Nov 23 '25 08:11

cute_programmer