Comprehensive summary of php Date and Time operations

Source: Internet
Author: User
Tags echo date
This article mainly sorts out the related content about the date and time calculation of php, which involves comprehensive knowledge points. If you are interested, please refer to it.

This article mainly sorts out the related content about the date and time calculation of php, which involves comprehensive knowledge points. If you are interested, please refer to it.

Before explaining the examples, we will introduce several core functions:
Mktime Function
The mktime () function returns the Unix timestamp of a date.
The parameter always represents the GMT date, so is_dst has no effect on the result.
The parameters can be left empty from right to left. The blank parameters are set to the corresponding current GMT value.
Syntax:Mktime (hour, minute, second, month, day, year, is_dst)
Parameter description
Hour is optional. The specified hour.
Minute is optional. Minutes.
Second is optional. Specified seconds.
Month is optional. Indicates the month in number.
Day is optional. Specified day.
Year is optional. Specified year. In some systems, the valid value ranges from 1901 to 2038. However, this restriction does not exist in PHP 5.
Is_dst is optional. If the time is in the daylight saving time (DST) period, it is set to 1; otherwise, it is set to 0. If it is unknown, it is set to-1.
The is_dst parameter has been deprecated since 5.1.0. Therefore, the new time zone processing feature should be used.
Example: The mktime () function is very useful for date calculation and verification. It can automatically correct out-of-range input:

<? Php echo (date ("M-d-Y", mktime (,); echo (date ("M-d-Y", mktime, ,); echo (date ("M-d-Y", mktime ))); echo (date ("M-d-Y", mktime (,);?>

Output:

Jan-05-2002 Feb-01-2002 Jan-01-2001 Jan-01-1999

Strtotime Function
The strtotime () function parses the date and time descriptions of any English text into Unix timestamps.
Syntax:Strtotime (time, now)
Parameter description
Time specifies the time string to be parsed.
Now is used to calculate the timestamp of the returned value. If this parameter is omitted, the current time is used.
One week later: strtotime ("+ 1 week ");
One week ago: strtotime ("-1 week ");
After December January: strtotime ("+ 1 months ");
One day later: strtotime ("+ 1 days ");
30 seconds later strtotime ("+ 30 seconds ");
After 20 minutes, strtotime ("+ 20 minutes ");
Strtotime ("+ 12 hours") after 12 hours ");

Date function
The date () function is used to format a local time/date.
Syntax
Date (format, timestamp)
Date_default_timezone_set Function
The date_default_timezone_set () function is used to set the default time zone of all date/time functions in the script.
Date_default_timezone_set (timezone)

Instance

First caseIf there is no database, and the obtained date value is compared, you have to use the php time and date function for computation, as shown below:

For example, to calculate the number of days from:

<? Php $ startdate = strtotime ("2015-9-5"); $ enddate = strtotime ("2015-9-18"); // The above php time and date function has changed the date to the timestamp, is converted to seconds. In this way, we only need to subtract the two values and then convert the second to the day. The comparison is simple, as shown below: $ days = round ($ enddate-$ startdate)/3600/24 ); echo $ days; // days indicates the number of days.?>

SecondChild Growth

<? Date_default_timezone_set ('Asia/Shanghai'); // The preceding statement sets the time zone. In fact, it is fine if no time zone is set. However, when zde is debug, a prompt will be displayed, indicating what unsafe functions to say... Add it. Echo date ('Y-m-d H: I: s '). 'Today is '. date ('y '). 'Year '. date ('W '). 'Week'; $ stime = '2017-11-03 '; echo"

*** Since birth ($ stime... :

"; Echo" Today is the first". Lnbsp (daysofnow ($ stime), 3 )."Days
"; Echo" Today is the first". Lnbsp (weeksofnow ($ stime), 3 )."Week
"; Echo" Today is the first". Lnbsp (monthsofnow ($ stime), 3 )."Months
"; Echo" Today is the first". Lnbsp (yearsofnow ($ stime), 3 )."Year
";/* $ Output = sprintf (" Today is the first% 03dDays
Today is the day <font color = red>% 03dWeek
Today is the day <font color = red>% 03dMonths
Today is the day <font color = red>% 03dYear
....
". Date ('w', $ ntemp )."
"; Return ($ ntemp-$ ftemp)/60/60/24/7;} function daysofnow ($ stime) {$ ftime = strtotime ($ stime ); return ceil (abs (time ()-$ ftime)/(60*60*24);} function monthsofnow ($ stime) {$ ftime = strtotime ($ stime); $ fmonth = date ('M', $ ftime); $ fyear = date ('y', $ ftime ); $ nmonth = date ('M'); $ nyear = date ('y'); $ result = ($ nyear-$ fyear) * 12 + $ nmonth-$ fmonth + 1; return $ result;} function yearsofnow ($ stime) {$ ftime = strtotime ($ stim E); $ fyear = date ('y', $ ftime); $ nyear = date ('y'); return $ nyear-$ fyear + 1 ;} // The following functions are only used with spaces. They are not the core content. They are only beautiful functions Lnbsp ($ data, $ num) {$ result = trim ($ data ); for ($ I = $ num; $ I >= strlen ($ data); $ I --) {$ result = ''. $ result;} return $ result;}?>

Case 3:You can use the following code tomorrow, next month, and next year:

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.