Weird Results from time()

software development

#1

I’ve been using time() in one of my scripts to generate ids for items placed into a database. Then later when they are displayed I used the following to get the month and the day:

$day = date(“d”, $id);
$month = date(“M”, $id);

I get anything from February 23 1997 to October 02 2019

What’s wrong here?


#2

It’s impossible to tell for sure based on just the code snippet you’ve given us, but I’ll offer up one thing that I would be sure to pay attention to: Make sure that the database column is numeric or that you are properly converting from an integer to a datetime in your queries.

On a somewhat tangential note, using time() to generate a primary key is probably not something that’s going to be a good long term idea. time() has a granularity of seconds, which means that if you get two INSERT operations within the same second, you’re going to get a failed unique constraint, and one of your users is going to get an error.


#3

Oddly the issue seemed to fix itself.

Also, the issue with using time to generate primary keys isnt much of a concern for me because its a single user app.