1

Im getting a result from a SQL query im running which outputs the following date ($mydate) (05/02/2011) .... It is formatted as dd/mm/yyyy ... When i run

date('l jS \of F Y',strtotime($mydate))

Im getting a completely different date to what is inputted, im gathering this is because im not using the mm/dd/yyyy format.

How would i go about swapping it around for it to work properly?

Cheers.

0

4 Answers 4

3

You could use strptime() to parse the date/time format. You could also convert the date string to YYYY-MM-DD which is always parsed correctly by strtotime. E.g:

$date = implode('-', array_reverse(explode('/', $date)));

But I agree with the other answers. Have a look at your database settings first. Virtually all databases are able to output YYYY-MM-DD format directly, and most will do so by default. See why yours doesn't.

Sign up to request clarification or add additional context in comments.

Comments

1

I wonder why your date is coming out of your DB as dd/mm/yyyy, what kind of DB are you using? If MySQL, a DATE field should output YYYY-MM-DD which will behave correctly with strtotime().

You could explode('/', $mydate) to manually split dd, mm and yyyy, and then reconstruct a new date string, or directly use the resulting numbers with mktime().

Comments

0

Here is a list of formats which strtotime() understands.

Which SQL outputs such a weird date format? Or did you format the date yourself?

Comments

0

If it always comes in that format, you can use

$newtime = preg_replace("/\d\d\/\d\d/\d\d\d\d/", "$3/$2/$1", $date);

To convert it to yyyy/mm/dd, then just plug that into strtotime()

It's definitely not the most elegant solution, I would actually suggest reworking the database (create a new column, run a script to read each entry, generate the new date, then input that in, delete the old date field, then rename the new one, so this only has to be done once).

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.