Convert PHP Date to MySQL Format

convert php date to mysql format


$date = mysql_real_escape_string($_POST['intake_date']);

1. If your MySQL column is DATE type:

$date = date('Y-m-d', strtotime(str_replace('-', '/', $date)));

2. If your MySQL column is DATETIME type:

$date = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date)));

You haven't got to work strototime(), because it will not work with dash - separators, it will try to do a subtraction.


Update, the way your date is formatted you can't use strtotime(), use this code instead:

$date = '02/07/2009 00:07:00';
$date = preg_replace('#(\d{2})/(\d{2})/(\d{4})\s(.*)#', '$3-$2-$1 $4', $date);
echo $date;

Output:

2009-07-02 00:07:00

Convert date in format MM/DD/YYYY to MySQL date


$newvalue = date('Y-m-d', strtotime($originalvalue));

Converting Date format in PHP

Use strtotime() and date():

$originalDate = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)" ;
$newDate = date("Y-m-d H:i:s", strtotime($originalDate));

(see strtotime and date docs on the PHP site).

or use DateTime:

<?php
$source = "Mon Apr 22 2013 12:16:00 GMT+0530 (India Standard Time)";
$date = new DateTime($source);
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
echo $date->format("Y-m-d H:i:s"); // 22 2013 12:16:00
?>

DEMO

Convert from MySQL datetime to another format with PHP

If you're looking for a way to normalize a date into MySQL format, use the following

$phpdate = strtotime( $mysqldate );
$mysqldate = date( 'Y-m-d H:i:s', $phpdate );

The line $phpdate = strtotime( $mysqldate ) accepts a string and performs a series of heuristics to turn that string into a unix timestamp.

The line $mysqldate = date( 'Y-m-d H:i:s', $phpdate ) uses that timestamp and PHP's date function to turn that timestamp back into MySQL's standard date format.

(Editor Note: This answer is here because of an original question with confusing wording, and the general Google usefulness this answer provided even if it didnt' directly answer the question that now exists)

PHP: convert date to datetime and insert it into mysql database?

Do one thing,

$mysqlDate = date('Y-m-d H:i:s', strtotime(str_replace("/","-",$_POST['php_date'])));
echo $mysqlDate;

Give it a try,

it should work.

Note:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.

Source link.

Your new concern answer,

$mysqlDate = date('Y-m-d H:i:s', strtotime(str_replace("/","-",$_POST['php_date']).' + '.rand(30,60*60*24*3).' seconds'));

php format date into mysql format for insert

You can try this

$str = "03.03.2021";
$date = DateTime::createFromFormat("d.m.Y", $str);
echo date_format($date,"Y-m-d");

convert date data into mysql date format

Yes, you can convert the date with strtotime();

$dateToday = date("d-m-Y");
$newDate = date("Y-m-d", strtotime($dateToday));

OUTPUT: 2012-12-27

And then you can store data to your database.

When you have to recover the date you can reverse this operation like this:

$dateFromDatabase = "2012-12-27";
$reverseDate = date("d-m-Y", strtotime($dateFromDatabase));

OUTPUT: 27-12-2012

(corrected "Y-m-d" to "d-m-Y" in 2nd date call)



Related Topics



Leave a reply



Submit