2

I have a little bit of code and it doesn't seem to be working correctly.

I'm trying to put it into the correct format for my database. I'm doing the following:

date('Y-m-d H:i:s', strtotime('24/05/2011'));

But every time I echo it, it returns:

1970-01-01 10:00:00

4 Answers 4

3

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

From the docmentation of strtotime

This should work

date('Y-m-d H:i:s', strtotime('05/24/2011'));
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting, I've never taken the time to find out how that works before. Is that from PHP.net's documentation?
If you examine the link, you will indeed find it's the official documentation.
1

Just a guess, but try this:

date('Y-m-d H:i:s', strtotime('24/05/2011 00:00:00'));

After testing it shows this works:

echo date('Y-m-d H:i:s', strtotime("24-5-2011"));

Comments

1

It means that your strtotime function is failing to parse that date. When it fails, it returns false which date reads as 0, which is the UNIX epoch. As Programmer XR suggested, you may need to switch your format a bit - if what he said doesn't work, try converting the datetime into this format:

date('Y-m-d H:i:s', strtotime('2011-05-24 00:00:00'));

Comments

0

First, it's not date() but strtotime(). Next, strtotime() is not a magic wand, reading your mind and understands whatever format.
It's just a function, accepting certain arguments. So, if it returns wrong value - check input parameters and read manual to see if they are correct.

Third, you don't need such complicated function anyway. All you need is a few string operations:

list($d,$m,$y) = explode("/",$date);
$date = "$y-$m-$d";

Understanding string operations is most important thing in using PHP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.