Page 1 of 1

How to post UNIX timestamp for today

Posted: Wed Jan 16, 2008 2:20 am
by tommy1987
I want to write something in my code which will generate a unix timestamp of that precise (up to seconds perhaps or even more precise) time eg.

$today_UNIX = mktime(today);
echo $today_UNIX;

Which would equal something like 22102007122323.

Can't seem to find the system variable for the date.
Many thanks,

Re: How to post UNIX timestamp for today

Posted: Wed Jan 16, 2008 2:28 am
by onion2k
If you only need the date you shouldn't be using a timestamp. If you really need the timestamp of midnight today though, you'll need to pass some arguments to mktime(). All the necessary values can be found using date().

Re: How to post UNIX timestamp for today

Posted: Wed Jan 16, 2008 2:35 am
by tommy1987
Well I need something that I can append to a temporary filename, since anyone can do this at any time I want to minimise the chance of two users being on at exactly the same time and creating a file with the same name and causing problems.

Hence just a string of numbers that represent that precise moment in time to make a file like so.. tmp_12122008121212.jpg .

Re: How to post UNIX timestamp for today

Posted: Wed Jan 16, 2008 2:48 am
by tommy1987
SOLVED: microtime()

Re: How to post UNIX timestamp for today

Posted: Wed Jan 16, 2008 2:50 am
by onion2k
tommy1987 wrote:Well I need something that I can append to a temporary filename, since anyone can do this at any time I want to minimise the chance of two users being on at exactly the same time and creating a file with the same name and causing problems.

Hence just a string of numbers that represent that precise moment in time to make a file like so.. tmp_12122008121212.jpg.
By "trying to minimise the chance" of something happening you're really saying "I'm hoping this never happens but it might!". The chance needs to be zero. I'd embed the user's id or username into the filename myself, and/or use an incrementing version number. Using a timestamp is too much of a risk.

Re: How to post UNIX timestamp for today

Posted: Wed Jan 16, 2008 6:13 am
by Kieran Huggins
just name the file it's own MD5 - then who cares about collisions? It would be the same file!

Re: How to post UNIX timestamp for today

Posted: Wed Jan 16, 2008 12:26 pm
by onion2k
Kieran Huggins wrote:just name the file it's own MD5 - then who cares about collisions? It would be the same file!
You'd have to do an extra check to make sure it's not a shared asset if you delete the file if the user is deleted. That could get messy if it ever went wrong.