0
votes

Consider the following scenario - I have a PHP server that receives incoming Ajax requests to retrieve records by 1 or more fields matching a search query.

The system is deployed across different servers and can have different date formats, as following:

Client 1 (Europe) - date format 22-02-2016 (dd-mm-yyyy)

Client 2 (US) - date format 02-22-2016 (mm-dd-yyyy)

*Each client can set their own custom date & time format in their admin panel.

So a query may be either:

{"first_name": "john","order_date":"22-02-2016"}

or

{"first_name": "john","order_date":"02-22-2016"}

or even

{"first_name": "john","order_date":"2016-02-22"}

Here is where the plot thickens - the system also supports custom fields therefore a query can even be:

{"first_name": "john","custom_field":"22-02-2016"}

Where custom_field is stored as a DATE field in the database but the PHP server does not know which field type it is (technically I could retrieve this info but trying to avoid it for now).

I want to be able to run a database query, without explicitly knowing if custom_field is a date field, and still support the different formats.

Ideally, I would be able to change the format used by the database to compare dates, based on the client format i.e I would be able to tell MySQL that if the query contains a date field, use a given format. Or change it globally in the table/database schema.

My alternatives are to iterate through the request and convert each date field from the client format to MySQL format or use regular expressions to convert the query dates. However, I would much rather solve it at the database level if possible.

1
You should fix the APIs so all dates are passed in ISO 8601 standard formats, such as YYYY-MM-DD.Gordon Linoff
@GordonLinoff thanks, that what I was afraid I would have to do. Is there no way to do this at the database level?dev7
Personally I'd go with a Unix timestamp. Faster queries and easier to manipulate on the frontend.Brian
What if I can't control how the client submits the date?dev7
@Yani, Convert them to a unix format prior to insert. mktime()Brian

1 Answers

0
votes

You can store the dates your users submit in a standard timezone, and then calculate back to the timezone you want it displayed in your php code.