Conversion and Calculation of UNIX Time Stamp and Day Period in PHP

  • 2021-08-05 09:00:48
  • OfStack

UNIX timestamp is a compact and concise method for saving date and time, a method for saving current date and time in most UNIX systems, and a standard format for representing date and time in most computer languages. Greenwich Mean Time is represented as a 32-bit integer, for example, using certificate 11230499325 to represent the timestamp of the current time. The UNIX timestamp is the number of seconds elapsed from 0:00 on January 1, 1970 (midnight at UTC/GMT) until the current time. Zero on January 1, 1970 is the basis for all date calculations, which is usually called UNIX era.

Because the UNIX timestamp is a 32-bit digital format, it is particularly suitable for computer processing, such as calculating the number of days between two time points. In addition, due to cultural and regional differences, there are different time formats and time zones. Therefore, UNIX timestamp is also a general format designed according to one time zone standardization, and this format can be easily converted into any format. Also, because the UNIX timestamp is represented by a 32-bit certificate, there will be some problems when dealing with events before 1902 or after 2038. In addition, under Windows, since the timestamp cannot be negative, an error occurs when using the timestamp function provided in PHP to process dates before 1970. To make PHP code portable, you must keep this point in mind.

Convert date and time to UNIX timestamp

In PHP, if you need to convert the date and time to an UNIX timestamp, you can call the mktime () function. The prototype of this function is as follows:


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

All parameters in this function are optional. If the parameters are empty, the current time is converted to UNIX timestamp by default. This is the same as calling the time () function directly to get the current UNIX timestamp. Parameters can also be omitted from right to left, and any omitted parameters will be set to the current values of the local date and time. If you only want to change the date and don't care about the specific time, you can set the parameters of the first three transition times to 0. mktime (). The function is very useful for date operation and verification, and it can automatically check the input that crosses the boundary. As shown below:


<?php
echo date("Y-m-d",mktime(0,0,0,12,36,2008))."\n";    // Date exceeds 31 Days, output after calculation 2009-01-05
echo date("Y-m-d",mktime(0,0,0,14,1,2010))."\n";     // Month over 12 Month, output after calculation 2011-02-01
echo date("Y-m-d",mktime(0,0,0,1,1,2012))."\n";      // No problem transformation, output results 2012-01-01
echo date("Y-m-d",mktime(0,0,0,1,1,99))."\n";        // Will 99 The year changed into 1999 In, 1990-01-01
?>

If you need to parse the date-time description of any English text directly into an UNIX timestamp, you can use the strtotime () function, whose circle is as follows:


int strtotime(string time[,int now])

The function strtotime () creates a time stamp for the eulogizing moment in English natural language, accepts a string containing a date format in American English and attempts to parse it into an UNIX time stamp (a description from January 1 197 0 00: 00:00 GMT), whose value is relative to the time given by the now parameter, or the current system time if no secondary parameter is provided. The function returns a timestamp if it succeeds, or FALSE if it does not. The comparison with mktime () is as follows:


<?php
echo date("Y-m-d", strtotime("now"));                  // Output the current timestamp
echo date("Y-m-d", strtotime("8 may 2012"));           // Output 2012-05-08
echo date("Y-m-d", strtotime("+1 day"));               // Output the current date plus 1 Days
echo date("Y-m-d", strtotime("last monday"));          // Output 2012-04-02
?>

The following example uses strtotime () function to write a countdown program for anniversaries to introduce the practical application of this function in project development. The example code is as follows:


<?php
$now =strtotime("now"); // Current time
$endtime= strtotime("2014-08-18 08:08:08"); // Set the graduation time and turn it into a timestamp
 
$second = $endtime-$now; // Get the timestamp (in seconds) from graduation time to the present time
$year = floor($second/3600/24/365); // Convert the number of years from this timestamp
 
$temp =$second-$year*365*24*3600; // Remove the seconds of the whole year from this timestamp, leaving the seconds of the month
$month=floor($temp/3600/24/30); // Convert the number of months from this timestamp
 
$temp=$temp-$month*30*3600*24; // Remove the seconds of the whole month from the timestamp, leaving the description of days
$day = floor($temp/24/3600); // Convert the remaining days from this timestamp
 
$temp=$temp-$day*3600*24; // Remove the seconds of the whole day from this timestamp, leaving the seconds of the hour
$hour = floor($temp/3600); // Convert the remaining hours from this timestamp
 
$temp=$temp- $hour*3600; // Remove the seconds of hours from the timestamp, leaving the seconds of minutes
$minute=floor($temp/60); // Convert the remaining score from this timestamp
 
$second1=$temp-$minute*60; // There are only seconds left in the end
 
echo " There is still a distance from training and graduation ($year) Year ($month) Month ($day) Days ($hour) Hours ($minute) Points ($second1) Seconds. ";
?>

Note: If the given year is in a two-digit format, the values 0-69 denote 2000-2069, and 70-100 denote 1970-2000.

Calculation of date

In PHP, the simplest way to calculate the length between two dates is to calculate the difference between two UNIX timestamps. For example, the PHP script receives the date of birth submitted by the user from the HTML form and calculates the age of this user. As shown below:


<?php
// Receive the year, month, day in the birth date submitted by the user from the form
$year = 1981;
$month = 11;
$day = 05;
$birthday = mktime(0,0,0,$month,$day,$year); // Convert the date of birth to UNIX Time stamp
$nowdate = time(); // Call time() Function to get the UNIX Time stamp
$ageunix = $nowdate -$birthday; // Object of the user's age by subtracting two timestamps UNIX Time stamp
$age = floor($ageunix/3600/24/365); // Will UNIX Time stamp divided by 1 The number of seconds in years to get the user's age
echo " Age: $age";
 
?>

In the above script, calling the mktime () function changes the user's birth date to the UNIX timestamp, and then calling the time () function to get the UNIX timestamp of the current time. Because the format of this date is expressed in integers, they can be subtracted. The UNIX timestamp obtained after calculation is divided by the number of seconds in one year, and the UNIX timestamp is converted into the unit of annual quantity.

PS: Here, we recommend another Unix timestamp conversion tool for this site, with the operation methods of Unix timestamp in various languages:

Unix timestamp (timestamp) conversion tool: http://tools.ofstack.com/code/unixtime


Related articles: