Unix Timestamp Using PHP

Good day everyone! Last time, I teach you on first parameter in date() function in PHP. Then now, I will teach you on dealing with the Unix Timestamp Using PHP .

Then second parameter to the date() function is the Unix Timestamp. Are you wondering what it means? Mostly, Unix systems stores the current time and date as a 32-bit integer containing number of seconds since midnight, January 1, 1970, GMT, also known as the Unix Epoch.

Unix timestamp are a compact way of storing dates and times.

In Windows, the range of the Unix Timestamp is more limited. A timestamp cannot be negative, so timestamp before 1970 cannot be used. To keep your code portable, you should bear this fact in mind.

Although this is a standard Unix convention, this format is still used by date() and a number of other PHP functions even of you are running PHP under Windows. The only difference is that, for Windows, the timestamp must be positive.

When you are converting a date and time to a Unix timestamp, you can use the mktime() function. The prototype are as follows:

int mktime ([int hour[, int minute[, int second[, int month[, int day[, int year[, int is_dst]]]]]]])

The parameters are fairly self-explanatory, with the exception of the last one, is_dst, which represents whether the date was in daylight savings time. You can set this parameter to 1 if it was , 0 if it wasn’t, or -1 (the default value) if you don’t know. In the case of using -1, PHP will try to figure it out based on the system it is running on. This parameter is optional, so you will rarely use it anyway.

If you don’t provide such parameters, you may set the current values by using the following codes:

[php]$timestamp = mktime();[/php]

The codes below returns the Unix Timestamp for the current date and time. You could also get the result by using the following codes:

[php]$timestamp = time();[/php]

The time() function always returns the Unix timestamp for the current date and time.

Another options was the date() function which are been already discussed. The format string “U” requests timestamp. The codes below is equivalent to the two previous ones:

[php]$timestamp = date(“U”);[/php]

You can also pass in a two- or four-digit year to mktime(). Two-digit values from 0 to 69 are interpreted as the years 2000 to 2069, and values from 70 to 99 are interpreted as 1970 to 1999.

The following codes illustrates the use of mktime():

[php]$time = mktime(12, 0, 0);[/php]

The codes above gives noon on today’s date.

[php]$time = mktime(0, 0, 0, 1, 1);[/php]

The code above gives the 1st January in the current year. Note that 0 (rather than 24) is used in the hour parameter to specify midnight.

You can also use mktime() for simple date arithmetic. The following codes shows as the example:

[php]$time = mktime(12, 0, 0, $mon, $day+30, $year);[/php]

The codes above adds 30 days to the date specified in the components, even though ($day+30) will usually bigger the the number of days in that month.

That’s all.

If you have questions regarding this tutorial entitled as “Using Unix Timestamp in PHP” feel free to ask us by commenting below or visit on our contact page. Thank you.

Related Article you may like: Date and Time

Leave a Comment