0

From a custom utility class, I get the format of a date variable in the following format: d-m-Y. My plan is to use Date('Y-m-d', strtotime($myCustomDate)) but I'm not sure that PHP will return me the right date every time. How can I be sure that my approach is safe?

How can I know that 02-03-2013 will not be transformed as mysql equivalent of 03 February 2013, when I need 02 March 2013.

How do you deal with this problem?

2 Answers 2

1

The safest way (other than using timestamps, which is a whole different story), would be to use the YYYY-MM-DD format, since there can not be the inverse ( ie YYYY-DD-MM is not defined), and MySQL understands it well.

You can then use PHP to convert it to any other desired format.

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

5 Comments

and YYYY-MM-DD is even sortable
@hexbot: isn't Y-m-dthe php equivalent of mysql YYYY-MM-DD? my problem is between php converts
to be safe, once you get it from MySQL convert to timestamp. All PHP functions use timestamps as their source, and you don't have to bother with formatting hassles.
I need to send it to mysql, not to get it from MySql
strtotime is the key to that, since that will essentially convert your date. See formats available here , they are made not to conflict.
0

If the format is always d-m-Y, then you can use a preg_split function to break the date into its component parts and then reformat it using date

<?PHP 
  $date = "3-13-2013";
  $date_parts = preg_split("/-/",$date);
  $newDate = Date("Y-m-d",mktime(0, 0, 0, $date_parts[0], $date_parts[1], $date_parts[2]));
?>

This will result in $newDate having the value 2013-03-13.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.