B
B
BonBon Slick2017-05-09 14:52:20
PHP
BonBon Slick, 2017-05-09 14:52:20

Date formatting 1, Jan, 1998?

I get this format: 1, Jan, 1998, I'm trying to convert:

gmdate("Y-m-d", strtotime("2, Jan, 1998"))
// а эту дату 10 Oct, 2007
$date = date_create('10 Oct, 2007');
dd( date_format($date, 'Y-m-d'));
//вернет 2017-10-10 
$date = new \DateTime('10 Oct, 2007');
        dd( $date->format('Y-m-d'));

For some reason, it does not accurately format, it will return 2017-01-02 to me.
Maybe the Carbon library can format correctly, or how to do it in pure PHP?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Teterin, 2017-05-09
@BonBonSlick

Yes, Carbon is a super library, I always use it, but it's just a wrapper over native PHP functions for working with dates. But it's faster to do this (pure PHP):

$date = \DateTime::createFromFormat('d, F, Y', '2, Jan, 1998');
echo $date->format('Y-m-d');

You just need to correctly specify the format of the input string with the date: php.net/manual/en/datetime.createfromformat.php
If you are doing on Laravel, then of course you need to use Carbon:
echo \Carbon\Carbon::createFromFormat('d, F, Y', '2, Jan, 1998')->format('Y-m-d');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question