Retrieve date from Mysql inverted - php

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
?>

Related

Format mySQL timestamp by changing date-month and remove seconds using PHP function?

I echo $istoriko['timestamp'] from mySQL and I get 2014-04-24 00:10:29
How can I change it to
24/04/14 (or 2014) 00:10
Thank you
Try this, its simple
You can set any date format now in date functions
echo date("d/m/y",strtotime($istoriko['timestamp']));
Follow functions can help you:
$date = '2014-04-24 00:10:29';
$date = date('d/m/Y H:i',strtotime($date));
echo $date

break d date from yyyy-mm-dd into mm,yyyy and dd as separate elements

I am storing the date in MySQL in the format yyyy-mm-dd. I wish to extract individual elements of the date i.e. day,month and year as separate elements
How should I go about this?
I am using php
You can do it using explode function in php.
explode('-', $date_obj);
You could make use of the DateTime class which has the createFromFormat.
Like this..
<?php
$date = DateTime::createFromFormat('Y-m-d', '2009-02-23');
echo $date->format('Y');//2009
echo $date->format('F');//February or use 'M' for Feb
echo $date->format('d');//23

How to get current date in codeigniter

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!

Format DATETIME from MYSQL database using PHP

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

PHP formatting data string to MySQL date format

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.

Categories