PHP time stamp and day conversion and calculation examples of UNIX, phpunix_php tutorial

Source: Internet
Author: User
Tags compact echo date

PHP time stamp and day conversion and calculation examples, Phpunix


Unix timestamps are 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 as a 32-bit integer, for example, a timestamp that represents the current time using certificate 11230499325. The UNIX timestamp is the number of seconds that have elapsed since the start of the current time from January 1, 1970 0 o'clock (Midnight UTC/GMT). January 1, 1970 Zero as the basis for all date calculations, this date is typically a Unix era.

Because the Unix timestamp is a 32-bit numeric format, it is especially useful 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, 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 be easily converted to any format. Also because the UNIX timestamp is represented by a 32-bit certificate, there will be some problems in handling events before 1902 or after 2038. Also, under Windows, because the timestamp cannot be negative, an error occurs when you use the timestamp function provided in PHP to process dates before 1970. To make PHP code portable, you must keep this in mind.

Convert date and time to UNIX timestamp

In PHP, you can call the Mktime () function if you need to convert the date and time to a UNIX timestamp. The prototype of the function is as follows:
Copy the 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 the current time is converted to a UNIX timestamp by default if the argument is null. In this way, the current UNIX timestamp function is the same as calling the time () function directly. 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 just want to change the date and don't care about the time, you can set the parameters for the first three transition times to 0.mktime () functions are useful for date arithmetic and validation, and it can automatically input the bounds of the administration. As shown below:

Copy the Code code as follows:
<?php
echo Date ("Y-m-d", Mktime (0,0,0,12,36,2008)). " \ n "; Date more than 31 days, calculated after output 2009-01-05
echo Date ("Y-m-d", Mktime (0,0,0,14,1,2010)). " \ n "; Month exceeds December, calculated output 2011-02-01
echo Date ("Y-m-d", Mktime (0,0,0,1,1,2012)). " \ n "; No problems with the transition, 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 it is necessary to parse the datetime description of any English text directly into a UNIX timestamp, you can use the Strtotime () function, which has a circle as follows:

Copy the Code code as follows:
int Strtotime (string time[,int now])

The function strtotime () can create the time stamp of the Acura moment in the natural language of English, accept a string containing the American English date format and attempt to parse it into a UNIX timestamp (as described from January 1 1970 00:00:00 GMT), Its value is relative to the time given by the now parameter, and the current time of the system is used if no secondary argument is provided. When the function executes successfully, it returns a timestamp, otherwise false. The comparison with Mktime () is as follows:

Copy the Code code as follows:
<?php
echo Date ("Y-m-d", Strtotime ("Now"); Time stamp for output now
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 the Strtotime () function to write an anniversary countdown program that describes the actual application of the function in project development, as shown in the following example code:

Copy the Code code as follows:
<?php
$now =strtotime ("Now"); Current time
$endtime = Strtotime ("2014-08-18 08:08:08"); Set graduation time, turn into timestamp

$second = $endtime-$now; Gets the time stamp (in seconds) from the graduation time to the current time
$year = Floor ($second/3600/24/365); Convert the number of years from this timestamp

$temp = $second-$year *365*24*3600; Remove the number of seconds from this timestamp for the whole year, and the number of seconds left in the month
$month =floor ($temp/3600/24/30); Convert Moon number from this time stamp

$temp = $temp-$month *30*3600*24; Remove the number of seconds from the time stamp for the remainder of the month, and the description of the day
$day = Floor ($temp/24/3600); Convert the remaining days from this timestamp

$temp = $temp-$day *3600*24; Remove the number of seconds in the day from this timestamp, and the number of seconds left in the hour
$hour = Floor ($temp/3600); The remaining hours are converted from this timestamp

$temp = $temp-$hour *3600; Subtract the number of seconds from the timestamp and the number of seconds left
$minute =floor ($temp/60); To convert the remaining points from this timestamp

$second 1= $temp-$minute *60; The last few seconds left.

echo "Graduation from training and ($year) years ($month) months ($day) days ($hour) hours ($minute) minutes ($second 1) seconds. ";
?>

Note: If the given year is a two-digit format, its value of 0-69 means that 2000-2069,70-100 represents 1970-2000.

Calculation of dates

In PHP, the simplest way to calculate the distance between two dates is to calculate the difference between the two Unix timestamps. For example, in a PHP script, you receive a birth date from an HTML form User submission and calculate the age of the user. As shown below:

Copy the Code code as follows:
<?php
Receive the year, month, day of the user's submitted birth date from the form
$year = 1981;
$month = 11;
$day = 05;
$birthday = Mktime (0,0,0, $month, $day, $year); Turn the date of birth into a Unix timestamp
$nowdate = time (); Call the time () function to get the UNIX timestamp for the current time
$ageunix = $nowdate-$birthday; Two timestamp subtraction gets 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 gets the age of the user
echo "Age: $age";

?>

In the above script, the call to the Mktime () function shifts from the user's birth date to the Unix timestamp, and then the time () function to get the UNIX timestamp for the current time. Because the format of this date is expressed in integers, you can subtract them. The Unix timestamp that was obtained after the calculation is divided by the number of seconds in a year, and the Unix timestamp is converted to the unit of the yearly amount.

http://www.bkjia.com/PHPjc/914040.html www.bkjia.com true http://www.bkjia.com/PHPjc/914040.html techarticle in PHP, the time stamp of Unix and the conversion and calculation of the day period, Phpunix Unix timestamp is a compact and concise way to save the date and time, and is the most Unix system to save the current date and ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.