PHP project: convert times to numbers

July 24, 2004 / Filed under: PHP

One of the major obstacles I had when creating this new version of my site was how to differentiate a group of timestamps, and order them chronologically – from earliest to latest – and then number them accordingly (01, 02, 03, etc).

Let’s say I have written three blog entries for today. The three entries all have different (and unique) times.

The timestamp of each entry makes EVERY entry unique because there is no possible way of having multiple entries with the same date AND time. Sure, they could easily have the same date (which is the case for each of the three entries), but their times are completely unique, because there is no possible way (that I know of) where I could update SO FAST, that more than one entry had the exact same time. It’s impossible. Agree?

The timestamp I am referring to is the MySQL default time format, which is hour:minute:second. An example looks like this: 21:43:09, which is 9:43 PM, and 9 seconds, but rarely are seconds mentioned.

Let’s say the times for each of my three updates, throughout the day, are as follows:

  • 09:32:12 (9:32 AM)
  • 12:18:54 (12:18 PM)
  • 16:19:10 (4:19 PM)

I updated my blog with new entries at each of those times.

Now... how can I CONVERT each of these times to numbers, starting with 1, and ending with however many timestamps there are for the day – in this case, 3. AND... they have to be in chronological order, from earliest to latest.

It’s more complicated than it appears.

This is what I want to end up with:

  • 09:32:12 (9:32 AM) should convert to 01
  • 12:18:54 (12:18 PM) should convert to 02
  • 16:19:10 (4:19 PM) should convert to 03

And, obviously, if there are more than three entries for the given day, the next consecutive number would be used for each consecutive entry.

How did I auto–generate these numbers?

Comments/Mentions