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.
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
$date = "31/01/2017";
$date = date("Y-m-d", strtotime($date));
For some the above code is not working and giving me the result: $date = 1970-01-01;
When I am fetching date from database and converting in the same manner its working. But not working when i want to convert "d/m/Y" format to "Y-m-d"
I can convert the date in other ways by exploding, but why not the above code is working..??
try like this
<?php
$date = "31-01-2017";
$date = date("Y-m-d", strtotime($date));
echo $date;
?>
Output:
2017-01-31
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a date value from the database and its format is: YYYY-mm-dd hh:mm:ss
I want to convert it to this format: YYYY/mm/dd hh:mm
Here is my code:
<?php
$datetime = $value['last_online']; //from db
$date = date('d-m-Y', $datetime);
$time = date('Gi.s', $datetime);
echo $date.' '.$time;
?>
But it's not working and is giving me an error of:
A non well formed numeric value encountered
I want to get rid of the minus signs and the seconds so my new date and time format would be YYYY/mm/dd hh:mm. How do I do it?
Any ideas?
Use like below
$datetime = $value['last_online']; //from db
$date = date('Y/m/d h:i', strtotime($datetime));
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.
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
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