PHP Date-time functions

Source: Internet
Author: User
Tags echo date time zones iso 8601 iso 8601 format php class

time stamp: 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. 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)

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 integer, you will encounter some problems when dealing with events before 1902 or after 2038. Also, under window, because the timestamp cannot be negative, an error occurs if you use the timestamp function provided in PHP to process dates before 1970. To make PHP code portable, you must keep this in mind

"Strtotime ()"

Strtotime () expects to accept a string containing the U.S. English date format and attempt to resolve it to a Unix timestamp (the number of seconds from January 1 1970 00:00:00 GMT) whose value is relative to the time given by the now parameter, and the current time of the system if no such parameter is supplied

int Strtotime (string $time [, int $now = time ()])
<?phpecho strtotime ("Now"), "\ n",//1488259922echo Strtotime ("Ten September"), "\ n";//968536800echo Strtotime ("  +1 Day ")," \ n ";//1488346322echo strtotime (" +1 Week ")," \ n ";//1488864722echo strtotime (" +1 Week 2 days 4 hours 2 seconds "), "\ n";//1489051924echo strtotime ("next Thursday"), "\ n";//1488409200echo strtotime ("Last Monday"), "\ n";//1488150000? >
<?php    $t = "1989-03-01 01:00:00";    echo Strtotime ($t);//604713600    date_default_timezone_set (' PRC ');    $t = "1989-03-01 01:00:00";    echo strtotime ($t);//604688400?>

"Time ()"

Time () returns the current Unix timestamp

int time (void)
<?php$nextweek = time () + (7 * + *);//7 days; Hours; mins; Secsecho ' Now:       '. Date (' y-m-d '). " \ n ";//now:2017-02-28echo ' Next Week: '. Date (' y-m-d ', $nextWeek). " \ n ";//next Week:2017-03-07echo ' Next Week: '. Date (' y-m-d ', Strtotime (' +1 Week ')). " \ n ";//Next week:2017-03-07?>

"Date ()"

Date () formats a local time/date and returns the string that is generated by the integer timestamp as a given format string. If no timestamp is given, the local current time is used. In other words, timestamp is optional and the default value is time ()

String Date (string $format [, int $timestamp])

The format parameter is as follows

Day d of the month, 2 digits with leading zeros 01 to the day of the 31D week, text representation, 3 letters Mon to the day ordinal of the Sunj month, no Leading 1 to 31l days of the week, full text format Sunday to Saturdayn ISO-8601 format number representation of the week The number of days in the Day 1 (representing Monday) to 7 (for Sunday) S after the month of the English suffix, 2 characters st,nd,rd or th.  You can use the day of the W week with J, the number represents 0 (for Sunday) to 6 (for Saturday) the day of the year 0 To the week of the 365-week W ISO-8601 format year, starting from Monday 42 (the 42nd week of the year) month F month, full text format, January or MA RCH January to Decemberm Digit month, with a leading 1 to 12M three letter abbreviation for the month Ja        N to decn number for the month, no leading 1 to 12t the number of days that a given month should have 28-31 years L If it is a leap year if it is 1, otherwise 0o ISO-8601 format years number 1999 or 200 3Y 4-digit year full representation 1999 Or a 2003y 2-digit number represents the year 99 or 03 time a lowercase morning and afternoon value AM or        PmA uppercase morning and afternoon values AM or PMB Swatch Internet standard 000 to 999g Hours, 12-hour format, no leading 1 to 12G hours, 24-hour format, no leading 00 to 23h hours, 12-hour format, with leading                                1 to 12H hours, 24-hour format, with leading 000 to 23i minutes of leading zeros                                          00 to 59s seconds, with leading 0 to 59u milliseconds                                   654321 time zone E time zone ID (PHP 5.1.0 new) Utc,gmt,atlantic/azoresi for daylight savings                         If daylight saving time is 1, the number of hours between 0O and GMT is separated by a colon between +0200p hours and minutes. +02:00t the time zone of the local machine EST,MDTZ the number of seconds of the offset-4320      0 to 43200 full date/time C  Date in ISO 8601 format 2004-02-12t15:19:21+00:00r RFC 822 format Date Th     U, Dec 16:01:07 +0200u the number of seconds since the Unix era (January 1 1970 00:00:00 GMT)
<?php//Sets the default time zone to use. Date_default_timezone_set (' UTC ') available from PHP 5.1;//tuesdayecho date ("L");//Tuesday 28th of February 05:41:19 Amecho date (' l DS \of F Y h:i:s a ');//july 1, is on a Saturdayecho "July 1, are on a". Date ("L", mktime (0, 0, 0, 7, 1, +));//Tue, 05:41:19 +0000echo date (date_rfc2822);//2000-07-01t00:00:00+00 : 00echo Date (Date_atom, mktime (0, 0, 0, 7, 1, 2000)); >
<?php    $t = time ();    echo Date ("Y-m-d h:i:s"). " <br> ";//2017-02-28 06:56:42    echo Date (" Y/m/d h:i:s ", $t)." <br> ";//2017/02/28 06:56:42    echo date (" Y year M D Day H:i:s ", $t)." <br> ";//February 28, 2017 06:56:42?>

"Mktime ()"

In PHP, if you need to convert the date and time to a UNIX timestamp, you can call the Mktime () function

Mktime () Unix timestamp, parameters can be omitted from right to left, any omitted parameters will be set at cost to the current value of date and time

int mktime ([int $hour = Date ("H") [, int $minute = Date ("i") [, int $second = Date ("s") [, int $month = Date ("n") [, int $day = Date ("J") [, int $year = Date ("Y") [, int $is _dst =-1]] []])
<?php//Prints something Like:2006-04-05t01:02:03+00:00echo date (' C ', Mktime (1, 2, 3, 4, 5, 2006));? >

Mktime () is useful for date calculation and validation, and it automatically calculates the correct value for an out-of-range input

<?php    $t = mktime (1, 0, 0, 1989);    echo Date (' y-m-d h:i:s ', $t). ' <br> ';//89-01-03 01:00:00    $t = mktime (1, 0, 0, 1989);    echo Date (' y-m-d h:i:s ', $t). ' <br> ';//89-02-12 01:00:00?>
Time

Each region has its own local time, in the network and the non-grade electrical communication, the time conversion problem appears particularly prominent. The entire Earth is divided into 24 time zones, each with its own local time. For the sake of unification, the use of a uniform time, known as Universal coordination (Universal time COORDINATED,UTC), is the global standard times set by the world time standard

The default time zone setting for PHP is UTC, and Beijing is in the East 8 of the time zone, leading UTC8 hours, so when you use functions such as time (), you don't get the right times. There are two ways to modify the time zone

1. Modify the configuration file

If you are using a standalone server with permissions to modify the configuration file, setting the time zone can be done by modifying the Date.timezone property in PHP.ini, and you can set the value of this property to "Asia/shang", "asia/chongqing", "etc/ One of the GMT-8 "or" PRC "

2. Calling functions

You can set the time zone by calling the function Date_default_timezone_set () function

Date_default_timezone_set (' PRC ');
<?php    echo Date ("Y-m-d h:i:s"). " <br> ";//2017-02-28 07:06:05    date_default_timezone_set (' PRC ');    echo Date ("Y-m-d h:i:s"). " <br> ";//2017-02-28 14:06:05?>

The Date_default_timezone_get () function can be used to get the current default time zone

<?php    Echo Date_default_timezone_get ();//europe/paris    date_default_timezone_set (' PRC ');    Echo Date_default_timezone_get ();//prc?>

Microseconds

In PHP, most of the time format is represented by a Unix timestamp, while the UNIX timestamp is the unit of the smallest time of measurement in the heart. This is not accurate for some applications, so you can call Microtime () to return the current UNIX timestamp and the number of microseconds

Mixed Microtime ([bool $get _as_float])

If the Get_as_float parameter is given and its value equivalent to True,microtime () returns a floating-point number

<?php    Echo microtime (). " <br> ";//0.72119500 1488282853    Echo microtime (True);//1488282853.7212?>

The Microtime () function is commonly used for performance analysis

<?php    date_default_timezone_set ("PRC");    $start =  Microtime (true);    for ($i =0; $i <100000; $i + +);    $end = Microtime (true);    Echo $end-$start;//0.0067892074584961?>

Get time

The date () function described earlier is used to set the time, while the GETDATE () function is primarily used to get the time

Array getdate ([int $timestamp = time ()])

The function will derive an array of associative arrays containing date information according to timestamp. If no timestamp is given, the current local time is considered

The key list element in the returned associative array has the following

Key Name            Description                             return value example "seconds" the number of          seconds is                      0 to the "minutes"          minute number means                    0 to "hours" the            number of hours represents                    0 to 23 " Mday "The number             of days in the month is             1 to" Wday "the number             of days of the week is             0 (Sunday) to 6 (Saturday) The number of" Mon "              month represents                    1 to" year "             A 4-digit number represents the full year             1999 or 2003 "Yday" the             number of days of the year represents             0 to 365 "weekday" The          full text of the day of the week represents               Sunday to Saturday " Month "            full text representation of               January or March    January to December0 the                  number of seconds since the beginning of the Unix era, and the return value of time () and for date () is similar to the value    
<?phpdate_default_timezone_set (' PRC ');//array ([seconds] + 8 [minutes] = 4 [hours] = [Mday] = [W] Day] [2] [mon] = 2 [year] [yday] [] [weekday] [] [] []] [] = Tuesday [month] + February [0] = 1488 283448) Print_r (getdate ()). " <br> ";//array ([seconds] = 0 [minutes] = 0 [hours] = 0 [Mday] = 1 [wday] = 6 [Mon] + 1 [yea R] = [Yday] = 0 [Weekday] = Saturday [Month] = January [0] = 946656000) Print_r (getdate (strtotime (' 2000-1-1 0:0:0 ')); >
Calendar

Use the object-oriented notation below to complete a simple calendar control

<! DOCTYPE html>

PHP Date-time functions

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.