PHP/Oracle: Time representation PHP/Oracle: Time representation oracle oracle

PHP/Oracle: Time representation


If you don't need the date at all, then what you need is the interval day data type. I haven't had the need to actually use that, but the following should work:

interval day(0) to second(6)


The option you use (3) is the best one.

Oracle has the following types for storing times and dates:

  • date
  • timestamp (with (local) time zone)
  • interval year to month
  • interval day to second

Interval data types are not an option for you, because you care when to start and when you finish. You could possibly use one date and one interval but this just seems inconsistent to me, as you still have one "incorrect" date.

All the other options you mentioned need more work on your side and probably also lead to decreased performance compared to the native date type.

More information on oracle date types: http://docs.oracle.com/cd/B19306_01/server.102/b14225/ch4datetime.htm#i1005946


I think that the most correct answer to this question is totally dependant on what you are planning to do with the data. If you are planning to do all your work in PHP and nothing in the database, the best way to store the data will be whatever is easiest for you to get the data in a format that assists you with what you are doing in PHP. That might indeed be storing them as strings. That may sound ghastly to a DBA, but you have to remember that the database is there to serve your application. On the other hand, if you are doing a lot of comparisons in the database with some fancy queries, be sure to store everything in the database in a format that makes your queries the most efficient.

If you are doing tasks like heavy loads calculating hours worked, converting into a decimal format may make things easier for calculations before a final conversion back to hours:minutes. A simple function can be written to convert a decimal to and fro when you are getting data from the database, convert it to decimal, do all your calculations, then run it back through to convert back into a time format.

Using unix timestamps is handy when you are calculating dates, probably not so much when you are calculating times though. While there seem to be some positives using this, such as very easily adding a timestamp to a timestamp, I have found that having to convert everything into timestamps to calculations is pesky and annoying, so I would steer clear of this scenario.

So, to sum up:

  1. If you want to easily store, but not manipulate data, strings can bean effective method. They are easy to read and verify. For anythingelse, choose something else.
  2. Calculating as numbers makes for super easy calculations. Convertthe time/date to a decimal, do all your heavy hiting, then revert toa real time format and store.
  3. Both PHP's Datetime and Oracle's Date are handy, and there are somefantastic functions built into oracle and PHP to manipulate thedata, but even the best functions can be more difficult then addingsome decimals together. I think that storing the data in thedatabase in a date format is probably a safe idea - especially ifyou want to do calculations based on the columns within a query.What you plan to do with them inside PHP will determine how you usethem.
  4. I would rule option four out right off the bat.

Edit: I just had an interesting chat with a friend about time types. Another thing you should be aware of is that sometimes time based objects can cause more problems than they solve. He was looking into an application where we track delivery dates and times. The data was in fact stored in datetime objects, but here is the catch: truck delivery times are set for a particular day and a delivery window. An acceptable delivery is either on time, or up to an hour after the time. This caused some havoc when a truck was to arrive at 11:30pm and turned up 45 minutes later. While still within the acceptable window, it was showing up as being the next day. Another issue was at a distribution center which actually works on a 4:00AM starting 24 hour day. Setting up times worked for the staff - and consolidating it to payments revolving around a normal date proved quite a headache.