Date Format in PHP MySql MSSQL JQuery
Date format sometimes is a problem for the developers. When you pickup the date as dd/mm/yyyy format from web page and want to store in the Microsoft SQL server that has a field datatype date or datetime or datetime2 then it takes the date value as yyyy-mm-dd format.
In this case I faced a problem means an error message showing [see the screenshot]. Then I have used CONVERT function, Cast function even DATE_FORMAT function but the date inserted into the database like 1970-01-01 every time. Front-end is PHP-JQuery and Back-end is MS SQL Server 2014.
Finally I coded IN PHP
$MyDate = $_POST['MyDate']; //21/07/2015
$MyDate = substr((string)$MyDate, 6, 4) . '-' . substr((string)$MyDate, 3, 2) . '-' . substr((string)$MyDate, 0, 2); //2015-07-21
and it works... if $MyDate variable pickups date like 21/07/2015 then it will change like 2015-07-21 format to be inserted in the mssql server 2014.
You can use any field datatype(date or datetime or datetime2) for the date column.
if you want insert current date from the php to ms sql server then just do the following...
$currDt = date("Y-m-d H:i:s");You can do this in many ways.
Date Format in PHP MySql MSSQL JQuery
Reviewed by Ashok Sen
on
15:37:00
Rating:
1 comment:
You can use DATE_FORMAT function to format the date while fetching or selecting the record (date) from MSSQL SERVER 2014 using php, so code will be like the following
$MyDate = DATE_FORMAT($row['MyDate '], 'm/d/Y');
and it works great!
Post a Comment