Determine Earliest Business Date

Suppose a customer may sends his/her request at any day, any time. The request can only be processed during business hours (weekdays, 9am to 5pm). Given a time at which the customer makes his/her request, I wish to determine the day that the request can be processed.

Examples:

  • If request is received at 2010-03-01 13:00:00 (Monday), the request can be processed on the same day.
  • If request is received at 2010-03-01 17:00:00 (Monday), the request will be processed on the following day (2010-03-02), because the cut-off time is 5pm.
  • If request is received at 2010-03-05 17:00:00 (Fri), the request will be processed on the following Monday (2010-03-08). It has past the cut-off time, and the request will not be processed during weekends.
  • If request is received at 2010-03-07 00:00:00 (Sunday), the request will be processed on the following Monday (2010-03-08). The request will not be processed during weekends.

I have written a function that determine the earliest business date in PHP. It returns an integer result (Unix timestamp).

Enchancements:

  • Allow user to specify cut-off time
  • Check for public holidays

Posted in Tao Of Programming at March 8th, 2010. No Comments.

[PHP] Conversion to MySQL Datetime

MySQL supports DATE, DATETIME, and Timestamp data types. Format for DATETIME can be YYYY-MM-DD HH:MM:SS, YY-MM-DD HH:MM:SS, YYYYMMDDHHMMSS, and YYMMDDHHMMSS. MySQL is quite flexible with delimiter, for example, 98@12@31 11^30^45 is perfectly valid.

Quite often, user may input datetime in a certain format, your application may be manipulating Unix timestamp, and you save the datetime (in DB) in another format. strtotime is a flexible PHP function for converting user input to Unix timestamp. It can accept strings like “tomorrrow”, “20090831″, as parameter. It returns FALSE when it cannot parse the input parameter.

Another useful PHP function is date. Usually I use it to reformat the Unix timestamp to a nicer format for output, and also for saving into MySQL database.

This piece of code converts user’s input into a format suitable for saving into MySQL database. MySQL also has a number of useful functions for manipulating datetime.

Posted in Tao Of Programming at May 5th, 2009. No Comments.