Convert date format in SQL [duplicate] - php

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 7 years ago.
I have a string of dates in php (dd/mm/yyyy) that I want to convert to yyyy-mm-dd format.
I have tried :
UPDATE `table` SET `DATE_VAR` = convert(datetime,'19/03/2015', 121) WHERE `ID`=160
Can you help me?

If you want to do the date conversion in PHP. Then one of the solutions could be:
$originalDate = str_replace('/', '-', "19/03/2015");
$newDate = date("Y-m-d", strtotime($originalDate)); // This is your mysql compatible date...
You can use $newDate variable in query.
UPDATE `table` SET `DATE_VAR` = '$newDate' WHERE `ID`=160

object oriented style:
echo DateTime::createFromFormat('d/m/Y', $date)->format('Y-m-d');
where $date is the date in dd/mm/yyyy format.

Related

php convert date format Manual dd/mm/yyyy [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
Use date()
$sqldate = "2016-07-18";
$newDate = date("d-m-Y",$sqldate);
I also use
implode('-', array_reverse(explode('/', $sqldate)));
I am trying to convert a date from yyyy-mm-dd to dd-mm-yyyy (but not in SQL);
however I don't know how the date function requires a timestamp,
and I can't get a timestamp from this string.
I am getting particular date format. WHY? I tried get a date expected format, i tried php date but no success..
I excpected output
18/07/2016
$sqldate = "2016-03-21";
echo date('d/m/Y', strtotime($sqldate));
Use strtotime().
$sqldate = "2016-03-21";
echo $newDate = date("d/m/Y", strtotime($sqldate));
Output
21/03/2016
Live Demo : Click Here
$sqldate = "2016-03-21";
$newDate = date("d-m-Y",strtotime($sqldate));
this is what you need.

PHP change date format (date coming from MySQL's 'CURRENT_TIMESTAMP' [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I tried a few things and so far and no success.
I want to make dates that are yyyy-mm-dd look like mm/dd/yyyy
Initial value example, taken from MySQL: 2015-08-21 14:46:00
Here's what I got:
foreach ($draftOrders as $key => $value) {
$timestamp = strtotime($value['last_modified']);
// this one works (leaving it unchanged)
// however the slashes are not replacing the dash
// $new_date_format = date('Y/m/d H:i:s', $timestamp);
// fails, date becomes 1970-01-01 12:00 am
$new_date_format = date('m/d/Y H:i:s', $timestamp);
$draftOrders[$key]['last_modified'] = $new_date_format;
}
To perform that sort of date formatting in mysql you might want to look at the following:-
select date_format(`date`,'%Y/%m/%e %H:%i:%s') from `table`
try this
echo date('m/j/Y', strtotime('2015-08-21 14:46:00'));
http://php.net/manual/en/function.date.php

change date format from Y-m-d to d-m-Y [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
based on this code :
$tarikh = mysql_real_escape_string($_POST['tarikh']);
$tarikh = date('Y-m-d', strtotime($_POST['tarikh']));
can someone show me to convert the format from Y-m-d to d-m-Y format ? thanks
try this
$tarikh = mysql_real_escape_string($_POST['tarikh']);
$myDateTime = DateTime::createFromFormat('Y-m-d', $tarikh);
$newDateString = $myDateTime->format('d-m-Y');

Formatting date from dd-mm-yyyy to yyyy-mm-dd in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a string mm-dd-yyyy which i get from a form, which i want to store it in the database of the data-type DATE (yyyy-mm-dd).
How do i format the string and save it in the database ?
$new_format = date("Y-m-d", strtotime('04-28-2012'));
or
$date = new DateTime('04-28-2012');
$new_format = $date->format('Y-m-d');
or in PHP 5.5+
$new_format = (new DateTime('04-28-2012'))->format('Y-m-d');
Try
$date = DateTime::createFromFormat("m-d-Y", '02-15-2012');
echo $date->format('Y-m-d H:i:s') , "\n";
Output
2012-02-15 23:54:52

Convert yyyy/mm/dd-hh-mm-ss.00 to Y-m-d H:i:s in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I'm developing a system that recibe a date format like: yyyy/mm/dd-hh-mm-ss.00 and I need convert it to Y-m-d H:i:s.
What I done was:
$newDate = date("Y-m-d H:i:s", strtotime($values[2]));
where $values[2] it is the date in that format, for instace: 2014/01/01-13-18-23.00. But the result is: 1970-01-01 01:00:00
Can you help me?
Thanks
You can try with strptime like
$format = "%Y-%m-%d %H:%i:%s";
echo strptime($values[2], $format);
Try using DateTime
$date = DateTime::createFromFormat('Y/m/d-H-i-s.00', '2014/01/01-13-18-23.00');
echo $date->format('Y-m-d H:i:s');
See demo here
You should be able to do something like this:
$date = DateTime::createFromFormat("Y/M/d-H-i-s.00", $values[2]);
echo $date->format("Y-m-d H:i:s");
The dateTime format for strtotime() function are predefined and you have to follow the rules to get desired result.
Mixed date time formats are available here PHP: Compound Formats - Manual

Categories