20101109-240000
or
20101109-235959
How to convert the date format above into MySQL format, Y-m-d H:i:s
Thank You
$mysqldate = preg_replace('/(\d{4})(\d{2})(\d{2})\-(\d{2})(\d{2})(\d{2})/', '$1-$2-$3 $4:$5:$6',$date)
Use STR_TO_DATE() MySQL function :
SELECT STR_TO_DATE('20101109-235959', '%Y%m%d-%H%i%s');
-- 2010-11-09 23:59:59
P.S. 20101109-240000 wouldn't work because it is not a valid timestamp.
Related
I have MySQL database with field date format dd/mm/YYYY. As you know this date format is not working perfect with PHP.
How can I transform such (dd/mm/YYYY) into good-looking (YYYY-mm-dd). If there is a PHP function?
You can use the DateTime::createFromFormat method
$dateTime = DateTime::createFromFormat('d/m/Y', '25/02/2016');
echo $dateTime->format('Y-m-d');
You can format dates using PHP's date() function.
$date = "2016-03-21";
$newDate = date("Y-m-d", strtotime($date));
you can fetch date directly from query by using mysql function DATE_FORMAT(date_column,"%d-%m-%Y")
select DATE_FORMAT(date_column,"%d-%m-%Y") as date from table
this will return date in 01-02-2016 format
I have dates formatted as d/m/y. How can I insert them into a DATETIME column?
Use MySQL's STR_TO_DATE() function:
INSERT INTO my_table VALUES (STR_TO_DATE('26/5/12', '%e/%c/%y'))
You need to use php's date() function along with strtotime() to convert date to any format you want.
MySQL database stores the date in YY-MM-DD format for datetime datatype, so if for example you have a date
$date = '26/05/2012';
You can convert it by using date() and strtotime()
$formatDate = date('Y-m-d', strtotime('26/05/2012'));
This will convert the date from 26/05/2012 to 2012-05-26 which then can be inserted into the database.
If you are using a timestamp datatype to store the date in your database, then all you need is to convert the current date into unix timestamp and store in database for example.
$date = strtotime('26/05/2012');
//this will convert the date to unix timestamp
Update:
as pointed out by #wallyk (thank you), strtotime() does not handles dd/mm/yy format. the fix is to replace the slash / by -m below code should work for you.
date('Y-m-d', strtotime(str_replace('/', '-', '26/05/2012')));
Try this:
$mysqldate = date("m/d/y g:i A", $datetime);
$date = date('d/m/Y');
$date = strtotime($date); //in unix time stamp format
Basically american date format is MM/DD/YYYY and you are providing DD/MM/YYYY so thats why startotime() returns you a null values on this input; and i prefer you must follow standard date format of american (MM/DD/YYYY) because if you are using mentioned format of date that will create more problems as well in different places ..
if you check by this
echo date('Y-m-d', strtotime('05/26/2012') );
and it is working fine ..
you could change your DATE column into a String Column and insert the data when ever you want 2 check if the date is right you can use a regular expression to do so
I currently have a datetimestamp in the following format when posted from a form: dd/mm/yyyy hh:mm however I am trying to convert it to the appropriate format before MySQL insertion which is in the following format: yyyy-mm-dd hh:mm:ss. The ss seconds will always be 0.
Is it correct to use strtotime?
I think it's safer to use strptime, because of month-day-year / day-month-year inconsistence.
print_r(strptime($date_in_mdy_format, '%m/%d/%Y %H:%M'));
yes so you should use the following:
$form_date = $_POST['form_date'];
$date_insert = date('Y-m-d H:mm:0', strtotime($form_date));
Yes you can use strtotime, and that would be
$mysqldate = date('Y-m-d H:i:s', strtotime($mydate));
Then you put the $mysqldate in the SQL query.
Also see the manual .
I have a date stored in a MySWL database in the following format.
2011-08-23 00:00:00
I'm then pulling that date in with php like so
echo $row['start'];
I want to edit the PHP code to display the date in the following format.
08/16/2011 12:00 am
Is there an easy way to do this?
I prefer:
echo date('m/d/Y g:ia',strtotime($row['start']));
Parse the timestamp stored by MySQL with strtotime to a unix timestamp, then use the date function to format it.
echo date('m/d/Y g:ia', strtotime($row['start']));
date_format is your friend.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
select date_format('2011-08-23 00:00:00','%m/%d/%Y %I:%i %p') -- 08/23/2011 12:00 AM
I want to convert user-submitted date format (mm/dd/yyyy) to a MySQL date format (YYYY-mm-dd). Submission is via a simple PHP form direc tto MySQL database.
$mysql_date = date('Y-m-d H:i:s', strtotime($user_date));
An alternative method as of PHP 5.2
$datetime = new DateTime($user_date);
echo $datetime->format('Y-m-d H:i:s');
DateTime is Y38k and timezone friendly.
A further method, this time on the SQL side is to use the convert method in your sql query:
CONVERT(VARCHAR(11),$user_date,111)
//e.g.
SELECT CONVERT(VARCHAR(11),DATEFIELD,111) AS DATE
//or
SET DATEFIELD = CONVERT(VARCHAR(11),'".$user_date."',111)
See: http://www.w3schools.com/sql/func_convert.asp - the number at the end changes the type of date format, with 111 returning: 2006/12/30.