Reputation: 4343
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
Upvotes: 2
Views: 1907
Reputation: 157838
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.
Upvotes: 0
Reputation: 86336
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'));
Upvotes: 3
Reputation: 7696
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"));
Upvotes: 1
Reputation: 13614
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'));
Upvotes: 1