This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
convert date string to mysql datetime field
I have a DateTime column in MySQL database. I would like to convert Form text field data on POST (string) to DateTime before inserting into the database. I appreciate any suggestions.
Depends entirely on what format your POST data is in. Quick/dirty/probably-will-blow-up-and-steal-your-belongings method is
$date = date('Y-m-d H:i:s', strtotime($_POST['yourfield']));
date() function.
You can find the function in php.net
It depends on format of MySQL date time field and the format of the form field. Normally db datetime is Year-Month-day Hour:min:sec for example, "2011-10-24 14:25:20". Now to convert form submitted date string to datetime, do the following:
$date = strtotime($_POST['form_date']);
$DatabaseDate = date("Y-m-d H:i:s", $date);
Now you can use the above $DatabaseDate to insert datetime into database:
#mysq_query("INSERT INTO TEST_TABLE (id, date_test) VALUES (2,
'$DatabaseDate')");
Hope it helps.
instead of strtotime($_POST['yourfield'])
use the function mktime, in that way you dont need to worry about the string format
Related
I have a MySQL table containing date and time in one of its column as text (15/05/2018 11:05:40). I want to convert this string to DateTime format so that it can be used to fetch data based on the date in PHP.
Use DateTime::createFromFormat
$date = date_create_from_format('d/m/Y H:i:s', $time);
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I am storing date in yyyy-mm-dd in database. Now i want to display that date in frontend but while displaying date it should be in dd-mm-yyyy format. How to display it in this format please help <?php echo $data2['emp_dob'];?> . I dont want to change my current format of storing date in yyyy-mm-dd. please help me to display it in dd-mm-yyyy format only
date('d-m-Y',strtotime($Your_date));
I don't think it's strictly correct to say that you're storing dates in your MySQL database as yyyy-mm-dd. The internal representation may be something very different than this, but in any case your real question is how to format the date in a certain way. One option is to handle this in your actual MySQL query using DATE_FORMAT(), e.g.
SELECT DATE_FORMAT(date_col, '%d-%m-%Y')
FROM yourTable
use this function, which uses inbuilt function 'substr(str,start,length)'.
function getFormatDate($date){
$date = substr($date,8,2).'-'.substr($date,5,2).'-'.substr($date,0,4);
return $date;
}
getFormatDate($data2['emp_dob']);
if (preg_match("/([0-9]{4})\-([0-9]{2})\-([0-9]{2})/", $data2['emp_dob'], $rg))
$data2['emp_dob'] = $rg[3]."-".$rg[2]."-".$rg[1]
<?php
$ddmmyyyy = date("d-m-Y", strtotime($data2['emp_dob']));
?>
<?php echo date("d-m-Y",strtotime($data2['emp_dob']));?>
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I am taking the input in M-d-Y format from calender but I want to convert it into Y-m-d when it store in the database. What method can I use?
You can try this. strtotime is your friend.
echo date("Y-m-d", strtotime('Dec-02-2014'));
or mysql DATE_FORMAT function, but mysql stores dates in yyyy-mm-dd format.
I think it better to do it in Mysql itself. When we have so many function. Like below:
SELECT STR_TO_DATE('Jan-31-2014', '%b-%d-%Y');
You can use it for insert query like below:
Insert into tableName `date` = STR_TO_DATE(InputVal, '%b-%d-%Y');
For more info:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
Try this:
$date="05-25-2014";
print(date("y-m-d",strtotime($date)));
use the date() and strtotime() like:
$var = date('Y-m-d', strtotime($timestring))
Converting string to Date and DateTime
copy from: https://stackoverflow.com/a/6239010/2478144
Make not that there is a difference between using forward slash / and hyphen - in the strtotime function. to quote from php.net
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Formating an SQL timestamp with PHP
I have a column in my SQL table that has default current timestamp values. I am trying to output it in a more readable format using date().
However, no matter which format I use, I always get the same date: 1970-01-01 00:33:32
Here is an example value of the current timestamp from the DB: 2012-08-09 06:37:58
Below is the code I use to try to "convert" it to a readable format:
$value['current_date']// is the var from the database.(containing 2012-08-09 06:37:58)
$somevar = date('Y-m-d H:i:s', $value['current_date']);
Thanks in advance.
Your problem is that you are using the wrong value in the second parameter. The date() function expects a UNIX-style timestamp as the second parameter, not a string representation of a date. Use the strtotime() function to correct this:
$somevar = date('Y-m-d H:i:s', strtotime($value['current_date']));
As someone else pointed out, however, why are you bothering to do this formatting? The format style you want is already in the database. It should be as easy as:
echo $value['current_date'];
The problem that you are having is because the second argument of the date() should be a timestamp, and since you are passing the raw string containing 2012-08-09 06:37:58, php does not know what to make of it.
Modify your code as below and it should work:
$somevar = date('Y-m-d H:i:s', strtotime($value['current_date']));
You can now use any date formats in place of 'Y-m-d H:i:s' as you wish
First of all: Why formatting the timestamp if it already exists in the desired format?
Regarding the actual problem:
See http://de2.php.net/manual/en/function.date.php
date() only accepts unix timestamps (seconds since 1970-1-1 00:00:00 UTC) or no timestamp at all. if you want to work with a timestamp like you have you need to create an unix timestamp first with date_create(), mktime , or strtotime
This question already has answers here:
mysql converting text input to datetime field
(5 answers)
Closed 1 year ago.
I have two text fields, one for the date in 08/01/2012 format and a second field containing the time. I currently have the time field in the format 09:41am but I have some flexibility with it's format (if 24hr is easier for example).
I was planning on just concatenating the strings and then converting. Should I convert the date to 2012-08-01 first?
How can I end up converting to datetime (2012-08-01 09:41:00)? How to convert back out of it into a 08/01/2012 and 09:41am format?
SELECT STR_TO_DATE(concat('08/01/2012', '09:41am'),'%d/%m/%Y%h:%i');
On the database side, you can use:
STR_TO_DATE() using the specifiers in this table to convert into a database friendly format. Reference
DATE_FORMAT() will then return whatever part you want of that database time. Reference
On the PHP side you can use:
strtotime() - Parse about any English textual datetime description into a Unix timestamp
date - Format a local time/date
strotime will produce a Unix timestamp based on any string date/time that is passed to it. If you pass both your fields (08/01/2012 & 09:41am) it will produce a timestamp based on it. To reverse the process, you use date("m/d/Y H:ia").
$field1 = '08/01/2012';
$field2 = '09:41am';
$stamp = strtotime($field1 . ' ' . $field2);
echo date("m/d/Y H:ia", $stamp);
I have not tested this, but it should work.