zac1987
zac1987

Reputation: 2777

How to interpret the PHP time() function

Every time a user uploads a new picture, we rename the picture to the value of PHP's time() function, so our database doesn't have duplicate picture names (because there is no same year, month, day, hour, min and sec).

I need to know whether or not the value of time() includes the year. If not, then next year will allow duplicated picture names. How do I understand the return value of time(), for example 1301888225? How do I get year, month, day, etc. information out of that number?

Upvotes: 1

Views: 204

Answers (4)

Dejan Marjanović
Dejan Marjanović

Reputation: 19380

echo date("d.m.Y H:i:s", 1301888225);

//04.04.2011 03:37:05

Unix timestamp will work on 32bit machines until January 19, 2038 so you can safely name your pictures for a long time. What if 2 users upload a picture at the same second?

$filename = time()."_".mt_rand(0, 9999).".jpg";

Upvotes: 2

alex
alex

Reputation: 490273

What if two users upload a file in the same second? Why don't you just use the temporary name PHP assigns to the file?

Upvotes: 0

shinkou
shinkou

Reputation: 5154

It's stated in the manual:

int time ( void )
Returns the current time measured in the number of seconds since the

Unix Epoch (January 1 1970 00:00:00 GMT).

So you don't have to worry about the duplicated name issue. However, you should be aware of the year 2038 problem.

Upvotes: 0

Jason McCreary
Jason McCreary

Reputation: 72991

time() returns the current Unix timestamp.

  • It does include year information
  • You can pass it to many of the PHP date functions, such as date() to output time however you want

Read the more about the Unix timestamp

Upvotes: 3

Related Questions