This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I've search a lot of times and only get converting date from Y-m-d to d-m-Y like this.
$date = new DateTime('2000-01-01');
echo $date->format('d-m-Y H:i:s');
But I have a string in d-m-Y and want to converte to Y-m-d.
It's just the reverse but I don't know how to do.
===
My own answer
People said that my question is already questioned.
But people don't understand that my date var is a STRING and return erro when I tried what they say as a solution.
My date var is a STRING in this format DD/MM/YYYY, I discover the solution searching it in pt-br on google.
This is the solution.
function data_user_para_mysql($y){
$data_inverter = explode("/",$y);
$x = $data_inverter[2].'-'. $data_inverter[1].'-'. $data_inverter[0];
return $x;
}
Simple:
$date->format('Y-m-d H:i:s');
The documentation should help you.
Other option without DateTime, would be:
echodate('Y-m-d',strtotime('01-01-2000'));
Related
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a string like this "20180720171534449" which is a kind of time stamp, is there an easy way I can convert this using PHP and format it as a date or date and time that makes sense to a human?
TIA
Peter
You have an 'YmdHisv' format where v is miliseconds.
Miliseconds is not parsable (as I found out today) with date_create_from_format so you need to remove that first from the string with substr.
$s = "20180720171534449";
$date = date_create_from_format('YmdHis', substr($s,0,-3));
echo date_format($date, 'Y-m-d H:i:s'); //2018-07-20 17:15:34
https://3v4l.org/m1XNd
As Ghost pointed out milliseconds is parasble if using microseconds u instead.
$s = "20180720171534449";
$date = date_create_from_format('YmdHisu', $s);
echo date_format($date, 'Y-m-d H:i:s\.v'); //2018-07-20 17:15:34.449
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I'm writing some code a script, which has short dates like
14/12/17
30/11/17
20/11/17
I need to convert these to long date format so i used PHP date function and strtotime as below
echo date('d-m-Y',strtotime('14/12/17'));
But it always getting 01-01-1970 as output, but it should be 14-12-2017
anyone know how to convert this to a long date format please.
PS. Other question answers suggest change the date input format, but I cannot change date input since it's getting from another site
This is from the link I posted and OP says is not correct.
Originally posted by ceiroa.
Convert one date format into another in PHP
$myDateTime = DateTime::createFromFormat('d/m/y','14/12/17');
$newDateString = $myDateTime->format('d-m-Y');
Echo $newDateString;
https://3v4l.org/2AcAd
Use preg_replace to flip the day and month on the incoming dates, then use date as you want.
$orig = ['14/12/17', '30/11/17', '20/11/17'];
foreach ($orig as $s) {
echo date('d-m-Y', strtotime(preg_replace('#(\d+)\/(\d+)\/(\d+)#', '$2/$1/$3', $s)))."\n";
}
This question already has answers here:
Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL
(9 answers)
Closed 5 years ago.
So i am busy with coding a program though the date format is Y-m-d but i need it to be the european date format. thus having to be d-m-Y but i do not know how to do this since the files come directly from my database. echo $row['Datum'];if possible i'd love some help with this issue
Try date() like:
echo date('d-m-Y', strtotime($row['Datum']));
Ex:
$row['Datum'] = '2017-06-30';
echo date('d-m-Y', strtotime($row['Datum']));
// Output: 30-06-2017
Php Fiddle Link
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I've had a very good search on this topic but haven't managed to turn up a definitive answer that gives me what i need. What i'm trying to do is convert as follows (in PHP):
From: '2014-04-16 08:22:00.000'
To: '16/04/2014 08:22'
And then back again, does anyone have an idea as to how this might be achieved? I not using seconds so i don't need that portion of the format only d/m/Y m:i. The original format comes from an MSSQL DB datetime field and the converted format will show in a input field.
Many thanks in advance.
DateTime::createFromFormat is your friend.
$date = DateTime::createFromFormat('Y-m-d H:i:s.u', '2014-04-16 08:22:00.000');
echo $date->format('d/m/Y H:i');
OR
$date1 = new DateTime('2014-04-16 08:22:00.000');
echo $date1->format('d/m/Y H:i');
From DATETIME to PHP Date Formting:
date("WHATEVER", strtotime($mysql_result->date))
To DATETIME from date:
date('Y-m-d H:i:s', strtotime($time))
https://php.net/manual/en/function.date.php
https://php.net/manual/en/function.strtotime.php
Try to find your answer on google before:
http://www.php.net/manual/en/datetime.formats.date.php
or
Convert one date format into another in PHP
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have the following datetime 2014-02-05 17:12:48 stored in a php variable named $reportDb["reportDate"].
Now, It is in Y-m-d H:i:s format, I want it in d/m/Y H:i:s format. How can I set or reformat this variable?
I tried with datetime::createformat but It doesn't works.
$formatted = date("d/m/Y H:i:s", strtotime($reportDb["reportDate"]))
If you want another method. Even if i would prefer the one already mentioned.
Output 05/02/2014 17:12:48
PHP 5.3 and older:
$dt = new DateTime('2014-02-05 17:12:48');
echo $dt->format(''d/m/Y H:i:s'');
PHP 5.4+:
$dt = (new DateTime('2014-02-05 17:12:48'))->format(''d/m/Y H:i:s'');
with the variable:
$dt = (new DateTime($reportDb["reportDate"]))->format(''d/m/Y H:i:s'');
If I understand you right, $reportDb["reportDate"] is already a DateTime object.
If so, all you need is ...
$reportDb["reportDate"]->format('d/m/Y H:i:s');
Cheers!