I have this string I'm getting from the GUI: 09/22/2012 (mm/dd/yyyy).
I want to format it to:
date("Y-m-d"); //(the MySQL DATE format )
What should be the right PHP date function parameters to do so?
You can try
$date = DateTime::createFromFormat("m/d/Y", "09/22/2012");
echo $date->format("Y-m-d");
Output
2012-09-22
You can also do the following to get the MySQL date format.
echo date('Y-m-d', strtotime('09/22/2012'));
// Output: 2012-09-22
Hope that helps.
Related
I am storing dates in my database like this: 2014-09-08. I want to retrieve them to my php script inverted: 08-09-2014. They are DATE.
How can I do this in my query? Or even in php if not possible.
Using MySQL, you can use DATE_FORMAT():
SELECT DATE_FORMAT(`date`, '%d-%m-%Y');
Using PHP, you can do this way:
echo date("d-m-Y", strtotime($row["date"]));
Fetch the date from the database and use date() function to format date. DEMO The demo outputs 08-09-2014.
<?php
$date = $row["date"]; //if value of date is 2014-09-08
echo date("d-m-Y", strtotime($date)); //outputs 08-09-2014
?>
I am writing a PHP form for my website. The HTML side asks the user for a date which they enter in MM/DD/YYYY format. When that string is sent to PHP the following code changes it to the form that MySQL will recognize
$date = $_POST['date'];
$sqldate = date('Y-m-d', strtotime($date));
However when that date is entered into my MySQL database it is entered as 1970-01-01 and I can't figure out why.
NOTE: If I echo $sqldate I get the error Use of undefined constant sqldate - assumed 'sqldate' in C:/MYDIRECTORY
How about this?
//build a date
$date = date_parse_from_format("m/d/Y", $_POST["date"]);
//output the bits
$sqldate = "$date[year]-$date[month]-$date[day]";
A Unix timestamp is the number of seconds since 1970-01-01 so your call to strtotime() is returning 0.
How is DateTime working for you?
<?php
$dateStr = '08/18/2014';//$_POST['date'];
$dateTime = new DateTime($dateStr);
echo $dateTime->format('Y-m-d');
You're writing that the date format of the form is MM/DD/YYYY. Do you validate the input values to be sure that the format is always given?
THis worked for me
$input_date= trim($_POST["input_date"]);
$strtotime= strtotime($input_date);
$date_format= date('Y-m-d',$strtotime);
How to get current date in codeigniter in YY-mm-dd format. I wants to get current date in YY-mm-dd frmat and put this value into input text box
You can use the PHP date function.
date('Y-m-d');
Up to my knowledge, there is no separate date function in codeigniter.
EDIT :
But if you want date in this format 13-04-05 [ yy-mm-dd ], Try this
date('y-m-d');
For more date formats, check this link PHP Date Formats
Try to use this is a generic format of DateTime
echo date('Y-m-d H:i:s');
use php date function
echo date("Y-m-d");
will give you the result
What about:
$date = new \Datetime('now');
var_dump($date);
if you want the full date:
echo date('Y-m-d');
depends your date structure.
echo date('d-m-Y');
if you want year only, then echo date('Y'); is enough
Use php date() function, like this
echo Date('Y/m/d');
it give you the desired result!
So I have a field in my database called 'DateTime' and the following lines of code:
echo "Date/Time: ";
echo $row['DateTime'];
How do I format it so that instead of being like this:'2013-02-07 22:14:56', it will be like this: '07/02/13 - 22:14'
Thanks.
Alternatively you could use:
DateTime::createFromFormat('Y/m/d H:i:s',$row['DateTime']); this will give you a datetime object, which are quite nice to work with.
Another alternative would be to have MySQL format the DATETIME value as a string in the desired format, using the DATE_FORMAT function.
SELECT DATE_FORMAT(`DateTime`,'%d/%m/%y - %H:%i') AS `DateTime`
...
No change required to your PHP code except for the SQL text sent to the database server.
This approach can very efficient, and reduce the amount of code you need, if all you are doing with this string is displaying it. If you are doing any sort of manipulation on this value, then casting the string value returned from MySQL resultset into a datetime object is probably a better way to go.
A demonstration of the DATE_FORMAT function:
SELECT DATE_FORMAT('2013-02-07 22:14:56','%d/%m/%y - %H:%i') AS `DateTime`
DateTime
----------------
07/02/13 - 22:14
how to output date into Year textbox Month textbox Day textbox
$book_date = $myrow["Publication_Day"];
$book_year = Date("Y", strtotime($book_date));
$timestamp contains ur date & time in any format.....................
date('Y/m/d - H:i',strtotime($timeStamp));
echo date('d/m/y H:i', strtotime($row['DateTime']));
See date and strtotime for more detail on the functions from the docs
$mytime = strtotime('2013-06-07 22:14:56');
$newDate = date('m/d/y - G:i', $mytime);
echo $newDate;
Here's an alternative using DateTime. If you're working with timezones this code can be easily modified to handle that.
$datetime = new DateTime('2013-02-07 22:14:56');
echo $datetime->format('d/m/y H:i');
See it in action
I have get data from oracle database and date value kept on 27-MAY-09. I need to insert this value to mysql database via PHP. I need to convert date format as 2009-05-27.
Any one know about it please let me know correct php statement for do this.
use date() function
$date = '27-MAY-09';
$newData = date('Y-m-d', strtotime($date));
php fiddle
$date = DateTime::createFromFormat('j-M-y', $inputDate);
$newDate = $date->format('Y-m-d');
PHP 5.3 not earlier.
try this
$date1 = "27-MAY-09";
$data2 = date("Y-m-d",strtotime($date1));