Examples of conversion and calculation of UNIX timestamps and dates in PHP, phpunix
UNIX timestamp is a compact and concise method of saving dates and times. It is a method of saving the current date and time in most UNIX systems and a standard for representing dates and times in most computer languages. Format. Represents Greenwich Mean Time as a 32-bit integer, for example, using certificate 11230499325 to represent a timestamp of the current time. The UNIX timestamp is the number of seconds that have elapsed since midnight on January 1, 1970 (midnight UTC/GMT) to the current time. Zero o'clock on January 1, 1970 serves as the basis for all date calculations, and this date usually becomes the UNIX epoch.
Because UNIX timestamp is a 32-bit digital format, it is particularly suitable for computer processing, such as calculating the number of days between two points in time. In addition, due to cultural and regional differences, there are different time formats and time zone issues. So UNIX timestamp is also a universal format designed to be standardized according to a time zone, and this format can be easily converted to any format. Also because the UNIX timestamp is represented by a 32-bit certificate, you will encounter some problems when processing events before 1902 or after 2038. In addition, under Windows, since the timestamp cannot be a negative number, an error will occur when using the timestamp function provided in PHP to process dates before 1970. To make your PHP code portable, you must keep this in mind.
Convert date and time to UNIX timestamp
In PHP, if you need to convert date and time into UNIX timestamp, you can call the mktime() function. The prototype of this function looks like this:
Copy code The code 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 will be converted into a UNIX timestamp by default. In this way, it has the same function as directly calling the time() function to obtain 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 convert the date and don't care about the specific time, you can set the first three conversion time parameters to 0. The mktime() function is very useful for date operations and verification. It can automatically correct out-of-bounds input. As shown below:
Copy code The code is as follows:
echo date("Y-m-d",mktime(0,0,0,12,36,2008))."n"; //The date is more than 31 days, and output after calculation 2009-01-05
echo date("Y-m-d",mktime(0,0,0,14,1,2010))."n"; //The month exceeds 12, and the calculated output is 2011-02-01
echo date("Y-m-d",mktime(0,0,0,1,1,2012))."n"; //No problem transformation, output result 2012-01-01
echo date("Y-m-d",mktime(0,0,0,1,1,99))."n"; // Will convert 99 years to 1999, 1990-01-01
?>
If you need to directly parse the date and time description of any English text into a UNIX timestamp, you can use the strtotime() function. The circle of the function is as follows:
Copy code The code is as follows:
int strtotime(string time[,int now])
The function strtotime() can create a timestamp of Acura Time in the natural language of English, accepting a string containing a US English date format and trying to parse it into a UNIX timestamp (since January 1 1970 00:00:00 GMT description), its value is relative to the time given by the now parameter. If no secondary parameter is provided, the current system time is used. If the function is executed successfully, it returns the timestamp, otherwise it returns FALSE. The comparison with mktime() is as follows:
Copy code The code is as follows:
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 day
echo date("Y-m-d", strtotime("last monday")); //Output 2012-04-02
?>
The following example uses the strtotime() function to write an anniversary countdown program to introduce the practical application of this function in project development. The sample code is as follows:
Copy code The code is as follows:
$now =strtotime("now"); //Current time
$endtime= strtotime("2014-08-18 08:08:08"); //Set the graduation time and convert it to a timestamp
$second = $endtime-$now; //Get the timestamp from graduation time to the current time (number of seconds)
$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 entire year from this timestamp, leaving only 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 only the description of the day
$day = floor($temp/24/3600); //Convert the remaining days from this timestamp
$temp=$temp-$day*3600*24; //Remove the whole day's seconds from this timestamp, leaving only the hour's seconds
$hour = floor($temp/3600); //Convert the remaining hours from this timestamp
$temp=$temp- $hour*3600; //Remove the seconds of the hour from the timestamp, leaving only the seconds of the minute
$minute=floor($temp/60); //Convert the remaining fraction from this timestamp
$second1=$temp-$minute*60; //There are only seconds left
echo "There are ($year) years ($month) ($day) days ($hour) hours ($minute) minutes ($second1) seconds before graduation.";
?>
Note: If the given year is in two-digit format, its value 0-69 represents 2000-2069, and 70-100 represents 1970-2000.
Calculation of dates
In PHP, the simplest way to calculate the length between two dates is to calculate the difference between two UNIX timestamps. For example, a PHP script that receives a user's date of birth from an HTML form and calculates the user's age. As shown below:
Copy code The code is as follows:
//Receive the year, month, and day of birth date submitted by the user from the form
$year = 1981;
$month = 11;
$day = 05;
$birthday = mktime(0,0,0,$month,$day,$year); //Convert birth date to UNIX timestamp
$nowdate = time(); //Call the time() function to get the UNIX timestamp of the current time
$ageunix = $nowdate -$birthday; //Subtract the two timestamps to obtain the UNIX timestamp of the user's age
$age = floor($ageunix/3600/24/365); //Divide the UNIX timestamp by the number of seconds in a year to get the user's age
echo "Age: $age";
?>
In the above script, the mktime() function is called to convert the user's date of birth into a UNIX timestamp, and then the time() function is called to obtain the UNIX timestamp of the current time. Because the date format is expressed using integers, they can be subtracted. The UNIX timestamp obtained after calculation is divided by the number of seconds in a year to convert the UNIX timestamp into annual units.
http://www.bkjia.com/PHPjc/914040.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914040.htmlTechArticleExamples of conversion and calculation of UNIX timestamps and dates in PHP. phpunix UNIX timestamps store dates and times. A compact and concise way to save the current date and...
on most UNIX systems