Datetime conversion format in PHP [duplicate] - php

This question already has answers here:
PHP Carbon determine date type from string format
(2 answers)
Closed 1 year ago.
What method should I use to convert 2021-03-20T00:19:07.000000Z to 2021-03-20 00:19:07, in PHP and Laravel 8.x, by the way, If you can explain what does the T in the middle of 2021-03-20T00:19:07.000000Z and the dot behind each represent? I will very much appreciate you !!

You can use Carbon date library in php and laravel.
Carbon::parse('2021-03-20T00:19:07.000000Z')->format('Y-m-d H:i:s')

Your input date is an ISO 8601 formatted date.
The T is the divider between date and time, so it is a static value.
To convert your ISO 8601 date to a YYYY-MM-DD hh:mm:ss date format, you can easily use DateTime (DateTime Documentation).
So in your case the solution would be:
$input = '2021-03-20T00:19:07.000000Z';
$datetime = new DateTime(input);
$output = $datetime->format('Y-m-d H:i:s');

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

Convert date to W3C format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have date format (YYYY-MM-DDThh:mm:ss.u):
2017-05-05T18:20:26.000Z
I need to convert it, so it will be without ms (.u) and to add GMT zone at the end of it (YYYY-MM-DDThh:mm:ss+TZD):
2017-05-05T18:20:26+00:00
How to do that? Should I use format() or date_format()?
Thank You!
You could use DateTime::createFromFormat().
$datestring = "2017-05-05T18:20:26.000Z";
$date = DateTime::createFromFormat("YYYY-MM-DDThh:mm:ss.u", $datestring);
Then use format() on $date as you need it.
DateTime::createFromFormat()
DateTime::format()

How to add day to a string in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
My current Date format is in MM/YY. I Need to default days to my format in php.
For example:
12/2009 -> 07/12/2009
I tried this code:
$currdate = '07/'.$currdate;
$newFormat = date('d-M-Y',strtotime($currdate));
But the new format is wrong, it output 12/07/2009.
----------------- Edit -----------------------------
I have tried **DateTime::createFromFormat**.Since my $currdate date format has only month and year its not accepting.I am getting a fatal error.
strtotime expects an american date format. Use datetime::createFromFormat instead:
$date = DateTime::createFromFormat('d/m/Y', $currdate);
echo $date->format('Y-m-d');
Edited to better explanation:
When you use date with slashes(/), PHP strtotime will think it is in m/d/Y format, the american way.
If you use dash (-) it will assume d-m-Y format.
If you use dot (.) it will assume Y.m.d format.

How to convert regional date into normal format date in PHP? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
hi have my : regional format date like : 01-Iulie-2014 or 01-Decembrie-2015 and I want to convert it into: 2014-07-01 or 2015-12-01. I tried something like this,but with no result:
$mydate = date('Y-m-d', strtotime($this->input->post('pay_date')));
but strtotime returns me false
Is there a function to do this stuff ?
strtotime() parses about any English textual datetime description into a Unix timestamp, so you could do, create an array of month names in your language and its equivalent english month name, and then use strtotime, as:
function custom_strtotime($your_date) {
//create month names as your_lang_month_name => english_month_name
$months_arr = array('Janvier'=>'jan',...,'Decembrie'=>'dec')
return strtotime(strtr(strtolower($your_date), $months_arr));
}
echo custom_strtotime($some_date);
From http://php.net/manual/en/function.date.php:
To format dates in other languages, you should use the setlocale() and strftime() functions instead of date().

Date format conversion results wrong date [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I tried converting
12-18-1997
to
18-12-1997
with this code
$new_date = date('d-m-Y', strtotime('12-18-1997'));
but it results in 18-12-1969
If I have to convert full date alongwith time then its converting fine but in the date I posted in question there is no time.
Use DateTime instead of strtotime():
$date = DateTime::createFromFormat( 'm-d-Y', '12-18-1997');
echo $date->format( 'd-m-Y');
You can see from this demo that it prints:
18-12-1997
strtotime is good, but it's not psychic or omniscient. you're feeding it a time string it's not able to parse properly:
php > var_dump(strtotime('12-18-1997'));
bool(false)
Since you simply assumed it's succeeding, you feed that false back to date(), where it's type-cast to an integer 0. However, your result is impossible, since int 0 as a date is Jan 1/1970. With timezone conversions, it'd be 31-12-1969 for you, NOT 18-12.
If you can't feed strtotime a format it understands, then use date_create_from_format and TELL it what what the format is:
$date = date_create_from_format('m-d-Y', '12-18-1997');
$text = date('d-m-Y', $date);

Categories