alim1990
alim1990

Reputation: 4972

Angular Material 6 date picker is not being converted in PHP to Y-m-d format

I have this angular material date picker:

<mat-form-field color="warn">
   <input matInput [matDatepicker]="picker" formControlName="add_date" placeholder="Date">
        <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
   <mat-datepicker #picker></mat-datepicker>
</mat-form-field>&nbsp;

This value is not readable with MysQL as is not the required format Y-m-d, so I convert the date sent to PHP like the following:

$dateAdded = strtotime('y-m-d', $dateAdded);
$dateAdded = date('Y-m-d', $dateAdded);

And everything added to database now is having the date of: Jan 1, 1970.

The format sent from Angular is like:

date: Fri Sep 14 2018 00:00:00 GMT+0300 (Eastern European Summer Time)

enter image description here

Upvotes: 0

Views: 622

Answers (1)

Exterminator
Exterminator

Reputation: 1246

To change the format of the date you do like this

$dateRequired=date('Y-m-d', strtotime($your_date));

In ts file you can add this code to get the date in the correct format

let d=new Date(date value);
let actualDate=d.getDate();
let actualMonth=d.getMonth()+1;
let actualYear=d.getFullYear();    
let formattedDate=actualDate + '/' + actualMonth + '/' + actualYear;

Upvotes: 3

Related Questions