How to post UNIX timestamp for today

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

How to post UNIX timestamp for today

Post 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,
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How to post UNIX timestamp for today

Post 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().
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Re: How to post UNIX timestamp for today

Post 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 .
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Re: How to post UNIX timestamp for today

Post by tommy1987 »

SOLVED: microtime()
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How to post UNIX timestamp for today

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: How to post UNIX timestamp for today

Post by Kieran Huggins »

just name the file it's own MD5 - then who cares about collisions? It would be the same file!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: How to post UNIX timestamp for today

Post 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.
Post Reply