Topic: How to solve the timestamp bug ?

Hello, as you may already know, there will be a bug in the date system after 2038, due to the way the dates are coded in databases :
"What is the unix time stamp?
The unix time stamp is a way to track time as a running total of seconds. This count starts at the Unix Epoch on January 1st, 1970 at UTC. Therefore, the unix time stamp is merely the number of seconds between a particular date and the Unix Epoch. It should also be pointed out (thanks to the comments from visitors to this site) that this point in time technically does not change no matter where you are located on the globe. This is very useful to computer systems for tracking and sorting dated information in dynamic and distributed applications both online and client side.

What happens on January 19, 2038?
On this date the Unix Time Stamp will cease to work due to a 32-bit overflow. Before this moment millions of applications will need to either adopt a new convention for time stamps or be migrated to 64-bit systems which will buy the time stamp a "bit" more time."

I've made some tests and when i put a date of 2038 in a post, it changes to 1903 or something like that...
How much the 64 bits systems will delay the bug of the timestamp coding ?
Thanks,

Pierre

Re: How to solve the timestamp bug ?

Nobody knows ?

3 (edited by PanBB.Ru 2016-05-24 10:33)

Re: How to solve the timestamp bug ?

There are 2 ways to solve this problem:
1 - Do nothing, because in 2038 the problem will be solved in a natural way;
2 - Use the built-in class «DateTime».


The first way to completely exhaust itself and, despite its simplicity, is short-sighted.
The second method, by contrast, has a right to exist. About him, and will be discussed further.

Class «DateTime», introduced in PHP 5.2, is designed to solve the problem completely in 2038. However, programmers who are accustomed to the standard features the date and time of processing, working with this class will be uncomfortable.

use function is the interface to the class of «DateTime» To speed up the application development process is best.

To make life easier for developers, I wrote a few analogues frequently used functions to work with dates and times. It should be noted that the input parameters and the behavior of the functions is not the same built-in counterparts.

Function to get current Unix timestamp

function enhanced_time()
{
  $DateTime_obj = new DateTime();
  return $DateTime_obj->format("U");
}

date and time display function in a specific format

function enhanced_date($format_str, $timestamp = NULL, $timezone = NULL)
{
  settype($format_str, "string"); 

  if (is_null($timestamp))
  {
    $timestamp = "now";
  }
  else 
  {
    $timestamp = "@" . number_format($timestamp, 0, ".", "");
  }

  if (!is_string($timezone))
  {
    $timezone = date_default_timezone_get();
  }

  $DateTime_obj = new DateTime($timestamp);
  $DateTime_obj->setTimezone(new DateTimeZone($timezone));

  return $DateTime_obj->format($format_str);
}

//
// Enhanced «getdate»
// (Function formatnut system date / time in GMT)
//
function enhanced_gmdate($format_str, $timestamp = NULL)
{
  return enhanced_date($format_str, $timestamp, "UTC");
}

The function information on a Unix timestamp

function enhanced_getdate($timestamp = NULL, $timezone = NULL)
{

  $arr = explode("=", enhanced_date("s=i=H=d=w=m=Y=z=l=F=U", $timestamp, $timezone));

  return array(
                "seconds" => (int) $arr[0], 
                "minutes" => (int) $arr[1], 
                "hours"   => (int) $arr[2], 
                "mday"    => (int) $arr[3], 
                "wday"    => (int) $arr[4], 
                "mon"     => (int) $arr[5], 
                "year"    => (int) $arr[6], 
                "yday"    => (int) $arr[7], 
                "weekday" =>       $arr[8], 
                "month"   =>       $arr[9], 
                0         =>       $arr[10]                
              );
}

Features of formation of Unix timestamp for a specified date and time

function enhanced_mktime($timezone = NULL, $hour = NULL, $minute = NULL, $second = NULL, $month = NULL, $day = NULL, $year = NULL)
{

  if (!is_string($timezone))
  {
    $timezone = date_default_timezone_get();
  }

  $default_datetime_arr = enhanced_getdate(NULL, $timezone);
  
  if (is_null($hour))   { $hour   = $default_datetime_arr["hours"]; }
  if (is_null($minute)) { $minute = $default_datetime_arr["minutes"]; }
  if (is_null($second)) { $second = $default_datetime_arr["seconds"]; }
  if (is_null($month))  { $month  = $default_datetime_arr["mon"]; }
  if (is_null($day))    { $day    = $default_datetime_arr["mday"]; }
  if (is_null($year))   { $year   = $default_datetime_arr["year"]; }

  settype($hour, "integer");
  settype($minute, "integer");
  settype($second, "integer");
  settype($month, "integer");
  settype($day, "integer");
  settype($year, "integer");

  $DateTime_obj = new DateTime();
  $DateTime_obj->setTimezone(new DateTimeZone($timezone));
  $DateTime_obj->setDate($year, $month, $day);
  $DateTime_obj->setTime($hour, $minute, $second);

  return $DateTime_obj->format("U");
}

function enhanced_gmmktime($hour = NULL, $minute = NULL, $second = NULL, $month = NULL, $day = NULL, $year = NULL)
{
  return enhanced_mktime("UTC", $hour, $minute, $second, $month, $day, $year);
}

Do not forget that now, with typical functions, without fail, should specify the default time zone. This should be done as follows:

date_default_timezone_set("Asia/Bangkok");

Also, here are a few options for the use of these functions:

$timestamp_now = time();
echo $timestamp_now . "
";
$timestamp_now_e = enhanced_time();
echo $timestamp_now_e . "
";
//
// Get and display information about the current Unix timestamp
//
echo "<pre>";
var_dump(getdate($timestamp_now));
var_dump(enhanced_getdate($timestamp_now_e));
echo "
";
//
// Display the current date and time in a specified format
//
echo date(«Y-m-d H:i:s», $timestamp_now). "
";
echo enhanced_date(«Y-m-d H:i:s», $timestamp_now_e). "

";
//
// Get and bring Unix timestamp for a distant future date and time
//
$year = 2050;
$month = 3;
$day = 18;
$hour = 22;
$minute = 56;
$second = 53;

$timestamp_future = mktime($hour, $minute, $second, $month, $day, $year);
echo $timestamp_future. "
";

$timestamp_future_e = enhanced_mktime(NULL, $hour, $minute, $second, $month, $day, $year);
echo $timestamp_future_e. "

";
//
// Print a future date and time in a specified format
//
echo date(«Y-m-d H:i:s», «2531257013»). "
";
echo enhanced_date(«Y-m-d H:i:s», «2531257013»). "

";

more info