Convert DATETIME issue [duplicate] - php

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

Related

What is this date format? 2021-10-04T08:19:54.000+04:00 [duplicate]

This question already has answers here:
What time stamp format is this?
(2 answers)
Closed 1 year ago.
And how can I covert it to 'd.m.Y H:i:s' with php?
gmdate('d.m.Y H:i:s', '2021-10-04T08:19:54.000+04:00')
did not help
The date format is ISO8601 if I'm not mistaken. PHP can parse this using the default DateTime class:
$date = new DateTime('2021-10-04T08:19:54.000+04:00');
$date->format('d.m.Y H:i:s');
For this purpose just use the native DateTime class. It can interpret several formats. Yours looks like ISO8601.
echo (new DateTime('2021-10-04T08:19:54.000+04:00'))->format('d.m.Y H:i:s');
its ISO 8601 date and you can format this easyly with dateTime or carbon.
You can find more info here https://www.php.net/manual/en/datetime.format.php

PHP Convert Date/Time Format to another [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a Date/time value as such;
2014-01-07T16:19:08Z
I wish to convert it to the format below using PHP
2015-03-21 02:12:01
I know I could use string replace twice over to remove the T and Z characters but doesn't seem right..
use strtotime() with date()
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
The date_format() function returns a date formatted according to the specified format.
$date=date_create("2014-01-07T16:19:08Z");
echo date_format($date,"Y-m-d H:i:s");
One more way is there
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
Update
The date_create() function returns a new DateTime object.
Reference
<?php
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
?>

Reformat datetime variable in PHP [duplicate]

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!

Converting php string d-m-y to date y-m-d [duplicate]

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'));

datetime to timestamp [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Inverse date function? - not strtotime
Is it possible to get UNIX time from such date 2011-02-27 02:04:46?
hello,
we have this function to convert a timestamp to datetime:
$datetime = date('Y-m-d H:i:s', $timestamp);
is there a function to do the opposite?
datetime to timestamp.
Thanks
$timestamp = strtotime($datetime);
Or if you're confident of the format, split up the string with explode() or even substr and pass the necessary parts into the mktime function.
Be aware that strtotime can sometimes get the timestamp wrong, if a slightly unconventional format is used.
EDIT:
A really accurate way of doing this is if you know your input format, is to use DateTime::createFromFormat eg:
$dateTimeObject = \DateTime::createFromFormat('G:i', '9:30');
$dateTimeObject->format('H:i');
See http://php.net/manual/en/function.date.php for formatting guides, and http://php.net/manual/en/datetime.createfromformat.php for info on the method described above.
$timestamp = strtotime('12-04-2010');

Categories