Unix Timestamp and PHP
This is a guide, you will not have a stand-alone effect or file by the end of this guide, but you will - hopefully - have an understanding of the Unix timestamp that allows you a powerful form of managing your PHP applications.
Note: This guide expects the reader to have an understanding on how MySQL databases work and a knowledge of basic PHP
Unix time, or POSIX time, is a system for describing points in time. It is widely used not only on Unix-like operating systems but in many other computing systems, including the Java programming language. It is an encoding of UTC, and is sufficiently similar to a linear representation of the passage of time that it is frequently mistaken for one.
The Unix timestamp is widely used in PHP. It is the amount of seconds between January 1st 1970 00:00:00 (Unix Epoch) and the present time, to the closest second. As you can imagine, after over 35 years it is now quite a big number!
Unix time is one of the things is handled differently on Windows servers compared to *nix servers. On a Linux server the timestamp can be positive or negative, representing before and after the Unix Epoch. Windows servers however produces -1 instead of a negative timestamp, or in PHP 5.1, it produces false.
PHP has many predefined functions that uses the Unix time, the functions that we'll be using are:
date() is likely the most-used date function in PHP, it can generate the current date or a selected timestamp in a huge amount of probabilities. A table of all the string determiners is available here
mktime() has parameters, one for each setting for time: second, minute, hour, month, day & year. The 7th parameter is for day light savings however if this setting is left alone PHP will find out the DS hour itself. mktime() returns a timestamp for the parameters made.
strtotime() converts a string into a timestamp, if it can't achieve this it'll return -1 or false.
time() returns the current time to the closest second as a timestamp.
You have a form that enters data into your mySQL database including the date, on a different webpage you need to call back just one of your database rows.
Solution: as the Unix timestamp changes every second they are virtually unique so using you can select a certain row with that timestamp, for example:
This solves the problem because all of the data for just one row is an array as $row ready to be outputted onto that page. The disadvantages of this solution is that the timestamp is a very exact and if the date called was even a second out it would not select the correct data.
You have a deadline for a piece of work and you want to know how many hours you have left.
Solution: you could use the mktime function to enter in the date of the deadline and subtract that timestamp with the current timestamp to find out how many hours are left.
You have a timestamp and you need to display that as a date in different formats.
That's it! I hope that if you followed this guide you have learnt something new about the PHP timestamp and I hope you can use this new knowledge in your next PHP project!
|All times are GMT +5.5. The time now is 01:29.|