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