PHP Classes

How to Implement a Year 2038 Problem Fix in PHP Applications that Are Using Unix Timestamps - Incredible Timestamp package blog

Recommend this page to a friend!
  All package blogs All package blogs   Incredible Timestamp Incredible Timestamp   Blog Incredible Timestamp package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement a Ye...  
  Post a comment Post a comment   See comments See comments (4)   Trackbacks (0)  

Author:

Viewers: 2,112

Last month viewers: 199

Package: Incredible Timestamp

Many PHP applications use Unix timestamps to make calculations with the dates and times of events.

However using Unix timestamps for these purposes will lead to a very big problem well known as the bug of year 2038.

Read this article to know more about the bug of year 2038 and how fix the issue with an alternative way to manipulate times and dates.




Loaded Article

Introduction

To explain the problem, this article explains several concepts related with UNIX timestamps. Parts of the article quote the Wikipedia.

What is a timestamp?

In computing, a timestamp refers to the use of a number to represent a time value, so you can establish temporal order among a set of events.

A timestamp is the time at which an event is recorded by a computer, not the time of the event itself. In many cases, the difference may be inconsequential: the time at which an event is recorded by a timestamp (e.g., entered into a log file) should be close to the time of the event.

This data is usually presented in a consistent format, allowing for easy comparison of two different records and tracking progress over time. The practice of recording timestamps in a consistent manner along with the actual data is called timestamping. The sequential numbering of events is also sometimes called timestamping.

Timestamps are typically used for logging events or in a sequence of events (SOE), in which case each event in the log or SOE is marked with a timestamp.

In file systems, a timestamp may mean the stored date and time of creation or modification of a file.

Timestamping techniques are used in a variety of computing fields, from network management and computer security to concurrency control. For instance, a heartbeat network uses timestamping to monitor the nodes on a high availability computer cluster.

What is UNIX Timestamp?

UNIX time (also known as POSIX time or epoch time) is a system for describing instants in time, defined as the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970 minus the number of leap seconds that have taken place since then.

It is used widely in UNIX-like and many other operating systems and file formats. Because the same timestamp can refer to two distinct instants of time around a leap second, it is neither a linear measure of time nor a true representation of UTC.

UNIX time may be checked on most UNIX systems by typing date +%s on the command line.

What are the limits of Unix timestamp?

The 32-bit representation of UNIX time will end after the completion of 2,147,483,647 seconds from the beginning (00:00:00 1 January 1970), on 19 January, 2038 03:14:08 GMT. This is referred to as the "Year 2038 problem" where the 32-bit UNIX time will overflow and will take the actual count to negative.

The Bug of the Year 2038

The Year 2038 problem is an issue for computing and data storage situations in which time values are stored or calculated as a signed 32 bit integer. This number is interpreted as the number of seconds since 00:00:00 UTC on 1 January 1970 (the epoch).

Such implementations cannot encode times after 03:14:07 UTC on 19 January 2038, a problem similar to but not entirely analogous to the Y2K problem (also known as the Millennium Bug), in which 2-digit values representing the number of years since 1900 could not encode the year 2000 or later.

Most 32-bit Unix-like systems store and manipulate time in this Unix time format, so the year 2038 problem is sometimes referred to as the Unix Millennium Bug by association.

Many data structures in use today have 32-bit time representations embedded into their structure. A full list of these data structures is virtually impossible to derive but there are well-known data structures that have the Unix time problem:

- File systems (many file systems use only 32 bits to represent times in inodes)
- Binary file formats (that use 32-bit time fields)
- Databases (that have 32-bit time fields)
- Database query languages, like SQL that have UNIX_TIMESTAMP() like commands

Examples of systems utilizing data structures that may contain 32-bit time representations include:

- COBOL systems of 1970s through 1990s vintage that have not been replaced by 2038 compliant systems
- Embedded factory, refinery control and monitoring subsystems
- Assorted medical devices
- Assorted military devices

Any system making use of data structures containing 32-bit time representations will present risk. The degree of risk is dependent on the mode of failure.

PHP and the UNIX Timestamp

Like many languages, PHP makes extensive use of the UNIX time and specially in his calendar standard functions. Many algorithms, for example, cookies expiration setting ,are based on this UNIX timestamp. The PHP functions like time(), mktime(), date() and many other functions used the Unix time to work.

Why an Alternative Could be Useful?

UNIX time, as we have seen, starts from the January 1, 1970 00:00:00, so if you were born before this date you cannot at this moment know exactly or even approximately the number of seconds you have spent on the Earth. It may be difficult to compute the time elapsed of your current age using the UNIX timestamp.

Many Web applications based on the PHP language propose to the users to compute their age and they really could not do it unless they used an alternative to PHP calendar functions that does not use the UNIX timestamp. Of course if you think you will buy your first yatch only in 2039, you cannot also know how many seconds remains until this purchase.

For these and other reasons that it could be useful to find an alternative to the UNIX timestamp and the many PHP calendar functions that rely on it.

How to Work Around the Bug of 2038 in Pure PHP?

For the moment there are no specific solutions for this problem. Of course there are many solutions but all are industrial like for example: replace all 32 bits systems by 64 bits before the year 2038. And this is the simplest solution computer scientists have found.

But do not panic, this article is here to provide help finding a remedy in a few steps for this matter and allow you to know how many seconds remains before you purchase your beautiful yatch or plane.

First step: Choose your conventions

UNIX time starts in1970, so we have choosen to help older guys by start our timestamp solution in year 0. So our beginning point will be the first january of year 0 at 00:00:00.

Second step: the algorithm

- First we choose a date and time example 1970-09-09 00:00:00

- Then compute the number of leap year since year 0 to your chosen date here 1970: it give us 477 leap years. We will call this number $i. Note that we use the Gregorian algorithm for leap year if ($x modulo 4==0 && $x modulo 100 !=0 or $x modulo 400==0 ) it is a leap year else it is a common year.

- Now substract $i from the chosen year here 1970: this will give you the number of common year that we will call $j.

- Now we want to know the number of days elapsed since the first January of the chosen year here 1970: you must look out for the year could be a leap year.

A leap year (also known as an intercalary year or bissextile year) is a calendar year containing one additional day (or, in the case of lunisolar calendars, a month) added to keep the calendar year synchronized with the astronomical or seasonal year.

Because seasons and astronomical events do not repeat in a whole number of days, calendars that have the same number of days in each year drift over time with respect to the event that the year is supposed to track.

By inserting (also called intercalating) an additional day or month into the year, the drift can be corrected. A year that is not a leap year is called a common year. For example, in the Gregorian calendar, each leap year has 366 days instead of the usual 365, by extending February to 29 days rather than the common 28.

Look into your chosen date the month and take all the days since the first January of the year to this month - 1.

In our example we must take all the days since the first January 1970 to the 31 August ($month - 1 == (9 - 1) == 8 == august here).

1970 is not a leap year so we have:

$days = array(0, 31, 59, 90, 120,151,181, 212, 243, 273, 304, 334, 365);

and if we call $k this number we have $k = 243 days.

Now we must take the number of days elapsed since the start of the chosen month (here 9): and 9 days elapsed .We will call this number $l.

- Compute the number of days elapsed since the first January of year 0 to your chosen date and it will be really simple with all we have do since the beginning:

$days = ($i * 366 +$j * 365 + $k + $l;)

- Finally Compute the number of seconds elapsed since first day of year 0 at 00:00:00 to your chosen date:

$day =86400;
$hour = 3600;
$min = 60;
$sec = 1;

$timestamp = ($days * 86400 + $h(chosen date) * $hour + $m(chosen date) * $min + $sec) - 86400.

We substract 86400 seconds because the last day is not full and is just represented by the time.
and all is done.

For our chosen date 1970 - 09 - 09 00:00:00 we have:

62188819200 seconds.

If you found out the result is the same number then you are good.

Download PHP Classes to Implement Long Timestamps

Here you can find a solution for computing the seconds elapsed since the year 0 here. You can even reverse that. While waiting the next tutorial on how to find the precise date when you just got an Incredible timestamp you can already see an implementation here.

And for providing a complete alternative for UNIX timestamp in pure PHP you can see a very precise date and time difference between two dates based on the algorithm above here, also a very precise date difference between two dates based on the same algorithm, and by the same thought this Incredible Day of Week finder in pure PHP for any date.




You need to be a registered user or login to post a comment

Login Immediately with your account on:



Comments:

1. Year 0? - Lee McAuley (2017-08-15 17:47)
What is year 0?... - 3 replies
Read the whole comment and replies



  Post a comment Post a comment   See comments See comments (4)   Trackbacks (0)  
  All package blogs All package blogs   Incredible Timestamp Incredible Timestamp   Blog Incredible Timestamp package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement a Ye...