A UNIX timestamp is a compact and concise way to save dates and times, a way to save the current date and time in most Unix systems, and a standard format for representing dates and times in most computer languages. Represents Greenwich Mean Time in 32-bit integers, for example, using certificate 11230499325 to represent the timestamp of the current time. The UNIX timestamp is the number of seconds that elapse from January 1, 1970 0 o'clock (Midnight in utc/gmt) to the current time. January 1, 1970 horizon as the basis for all date calculations, this date is usually the Unix era.
Because the Unix timestamp is a 32-bit number format, it is especially useful for computer processing, such as calculating the number of days between two points. In addition, due to cultural and regional differences, there are different time formats, as well as time zone problems. So Unix timestamps are also a common format designed to standardize on a time zone, and this format can easily be converted to any format. Also because the UNIX timestamp is represented by a 32-bit certificate, there are problems with handling events prior to 1902 or after 2038. In addition, in Windows, because the timestamp cannot be negative, an error occurs when you use the timestamp function provided in PHP to process the date before 1970. This must be remembered to make the PHP code portable.
Convert date and time to UNIX timestamp
In PHP, you can call the Mktime () function if you need to turn a date and time into a UNIX timestamp. The prototype of the function looks like this:
Copy Code code as follows:
int mktime ([int hour [, int minute[,int second[,int month[,int day[int Year]]]]
All parameters in the function are optional, and if the argument is null, the current time is converted to a UNIX timestamp by default. This way, the direct call time () function gets the same current UNIX timestamp functionality. Parameters can also be omitted from right to left, and any omitted parameters are set to the current value of the cost date and time. If you only want to change the date, do not care about the specific time, you can set the first three transition time parameters to the 0.mktime () function for date operations and validation is very useful, it can automate the school administration out of bounds of input. As shown below:
Copy Code code as follows:
<?php
echo Date ("Y-m-d", Mktime (0,0,0,12,36,2008)). " \ n "; Date exceeds 31 days, calculated after output 2009-01-05
echo Date ("Y-m-d", Mktime (0,0,0,14,1,2010)). " \ n "; Month more than December, after calculation output 2011-02-01
echo Date ("Y-m-d", Mktime (0,0,0,1,1,2012)). " \ n "; No problem change, output results 2012-01-01
echo Date ("Y-m-d", Mktime (0,0,0,1,1,99)). " \ n "; Will turn 99 into 1999, 1990-01-01.
?>
If there is a need to parse the date-time description of any English text directly into a UNIX timestamp, you can use the Strtotime () function, which has a circle that looks like this:
Copy Code code as follows:
int Strtotime (string time[,int now])
function Strtotime () You can create a time stamp of the Acura moment in the natural language of English, accept a string containing the U.S. English date format and try to resolve it to a UNIX timestamp (a description from the January 1 1970 00:00:00 GMT), The time given by the value relative to the now parameter, or the current time of the system if no secondary argument is supplied. Returns a timestamp if the function succeeds, or false. The comparison with mktime () looks like this:
Copy Code code 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 current date plus 1 days
echo Date ("Y-m-d", Strtotime ("last Monday")); Output 2012-04-02
?>
The following example uses the Strtotime () function to write a day countdown program that describes the actual application of the function in project development, as shown in the sample code:
Copy Code code as follows:
<?php
$now =strtotime ("Now"); Current time
$endtime = Strtotime ("2014-08-18 08:08:08"); Set graduation time, turn it into a time stamp
$second = $endtime-$now; Time stamp (seconds) to get graduation time to current time
$year = Floor ($second/3600/24/365); To convert the number of years from this time stamp
$temp = $second-$year *365*24*3600; To remove the number of seconds in the year from this timestamp, the number of seconds remaining in the month
$month =floor ($temp/3600/24/30); From this time stamp the CCP converts the moon number
$temp = $temp-$month *30*3600*24; Remove the number of seconds from the time stamp and leave a description of the days left
$day = Floor ($temp/24/3600); Converts the remaining number of days from this timestamp
$temp = $temp-$day *3600*24; To remove the number of seconds from the time stamp, the number of seconds left in the hour
$hour = Floor ($temp/3600); Converts the remaining number of hours from this timestamp
$temp = $temp-$hour *3600; To remove the number of seconds from the timestamp, the number of seconds left in minutes
$minute =floor ($temp/60); To convert the remaining points from this timestamp
$second 1= $temp-$minute *60; The last few seconds left.
echo "Distance to graduate from training also has ($year) year ($month) month ($day) days ($hour) hours ($minute) ($second 1) seconds. ";
?>
Note: If the given year is a two-digit format, its value of 0-69 indicates that 2000-2069,70-100 represents 1970-2000.
Calculation of dates
In PHP, the easiest way to calculate the length of a two-date interval is by calculating the difference between two Unix timestamps. For example, in a PHP script to receive the date of birth from an HTML form user, calculate the age of the user. As shown below:
Copy Code code as follows:
<?php
To receive the year, month, and day of the date of birth from a user's submission from a form
$year = 1981;
$month = 11;
$day = 05;
$birthday = Mktime (0,0,0, $month, $day, $year); Convert Birth date to Unix timestamp
$nowdate = time (); Call time () function to get the UNIX timestamp for the current time
$ageunix = $nowdate-$birthday; Two time Stamp subtraction Unix timestamp for user age
$age = Floor ($ageunix/3600/24/365); To get the age of a user by dividing the UNIX timestamp by the number of seconds in a year
echo "Age: $age";
?>
In the above script, the call to the Mktime () function converts from the user's birth date to the Unix timestamp, and then calls the time () function to obtain the current time's Unix timestamp. Because the format of this date is expressed in integers, you can subtract them. The Unix timestamp is then divided by the number of seconds in a year, and the Unix timestamp is converted to the unit of the yearly amount.
PS: Here again for you to recommend a local UNIX timestamp conversion tool, with a variety of languages under the Unix time stamp operation method:
Unix Timestamp (timestamp) conversion tool:Http://tools.jb51.net/code/unixtime