B
B
BuBux2020-02-03 15:15:31
PHP
BuBux, 2020-02-03 15:15:31

date format in php?

$date1 = '13/12/2020';
$date2 = date("Y-m-d", strtotime($date1));
echo $date2;

This code returns '1970-01-01'. But as soon as I change $date1 = '12/12/2020' it gives '2020-12-12'. Where is the mistake?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
FanatPHP, 2020-02-03
@BuBux

The fact that in PHP there is no "date format".
There is a function strtotime that tries to guess what date you meant.
You never have to guess, you always have to know for sure.
Therefore, either change the original format, or parse it manually. date_create_from_format(), or if you're afraid of objects, then explode()

M
Moses Fender, 2020-02-03
@mosesfender

There are no 13 months in a year.

K
kikimarik, 2020-02-03
@kikimarik

13 month does not exist =)

D
Dmitry, 2020-02-03
@Astatroth

Because there is no 13th month on Earth.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question