PHP Date and time processing-Zheng Achi (cont.) _php Tutorial

Source: Internet
Author: User
Tags iso 8601 iso 8601 format
1.UNIX time Stamp
PHPD processing data, especially when the time type data in the database is formatted, it is necessary to convert the time type data into the UNIX timestamp for processing. Different database systems for time-type data
cannot be compatible with conversions, you need to convert time to UNIX timestamp. In this way, Beijing realizes the cross-platform of different database systems.
2. Time conversion to timestamp
You can use the Strtotime () function if you want to convert the date and time expressed in a string to the form of a timestamp.
The syntax format is as follows:
int Strtotime (string $time [, int $now])
For example:
Copy CodeThe code is as follows:
echo strtotime (' 2009-03-05 '); Output 1236211200
echo strtotime (' 2009-03-05 10:24:30 '); Output 1236248670
Echo Strtotime ("Ten September 2000"); Output 968544000
?>

Another date-acquired Unix timestamp function is the mktime () function,
The syntax format is as follows:
int mktime ([int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year]] []])
3. Get the date and time
1.data () function
is to convert the timestamp in the given format to a specific date and time string.
The syntax format is as follows:
String Date (string $format [, int $timestamp])
Description
$format specify the format of the date and time after conversion,
$timestamp is a timestamp that requires conversion, and if omitted, uses the local current time, which is the value of the time () function for the default value.
Time () function returns the timestamp of the current time
The value of the $format parameter of the date function is given in the following table.
Table 4.6 format codes supported by the date () function

Characters

Description

Example of return value

D

The day ordinal of a month with a leading zero 2-digit number

01~31

D

Day of the week, denoted by 3 letters

Mon to Sun

J

The day ordinal of the month without leading zeros

1~31

L

Day of the week, full text format

Sunday~saturday

N

ISO-8601 format number represents the day of the week

1 (Monday) (Sunday)

S

English suffix after the number of days of the month, expressed in 2 characters

St, ND, RD or th, can be used together with J

W

Day of the week, numbers indicate

0 (Sunday) (Saturday)

Z

The day ordinal of the year

0~366

W

Week of the ISO-8601 format year, starting from Monday

Example: 42 (42nd Week of the year)

F

month, full text format, such as January or March

January~december

M

A number represents the month, with a leading zero

01~12

M

Three letter abbreviation for month

Jan~dec

N

The number represents the month, without leading zeros

1~12

T

The number of days that a given month should be

28~31

L

Whether it is a leap year

If the leap year is 1, otherwise 0

O

ISO-8601 format year number. This is the same value as Y, except that if the ISO week (W) belongs to the previous or next year, then that year

Example: 1999 or 2003

Y

4-digit year in full representation

Example: 1999 or 2003

Y

Year 2-digit representation

Example: 99 or 03

A

Lowercase morning and afternoon values

AM or PM

A

Uppercase morning and afternoon values

AM or PM

B

Swatch Internet Standard Time

000~999

G

Hours, 12-hour format, no leading zeros

1~12

G

Hours, 24-hour format, no leading zeros

0~23

H

Hours, 12-hour format, with leading zeros

01~12

H

Hours, 24-hour format, with leading zeros

00~23

I

Number of minutes with leading zeros

00~59

S

Number of seconds with leading zeros

00~59

E

Time Zone Flag

Example: Utc,gmt,atlantic/azores

I

Whether it is daylight saving time

If daylight saving time is 1, otherwise 0

O

Hours of difference from GMT

Example: +0200

P

Difference from GMT (GMT), separated by a colon between hours and minutes

Example: +02:00

T

The time zone in which this machine resides

Example: EST,MDT

Z

The number of seconds for the time zone offset. The time zone offset in the west of UTC is always negative, and the time zone offset in the east of UTC is always positive

-43200~43200

C

Date in ISO 8601 format

2004-02-12t15:19:21+00:00

R

Date in RFC 822 format

Thu, Dec 2000 16:01:07 +0200

U

Number of seconds since the beginning of the UNIX era

Time () function

2.getdate () function
You can get an array of date and time information,
The syntax format is as follows:
Array getdate ([int $timestamp])
Description: The $timestamp is the timestamp to convert, and the current time is used if not given.
The function returns an array of date and time information based on $timestamp, with the key name and value of the array as shown in table 4.7

Key Name

Description

Examples of values

Seconds

The number of seconds is expressed

0~59

Minutes

The number of minutes indicates

0~59

Hours

The number of hours indicates

0~23

Mday

The number of days in the month indicates

1~31

Wday

The number of days of the week indicates

0 (for Sunday) (= Saturday)

Mon

Numeric representation of the month

1~12

Year

Full Year 4-digit representation

Example: 1999 or 2003

Yday

The number of days in a year indicates

0~365

Weekday

Full text representation of the day of the week

Sunday~saturday

Month

Full text representation of the month

January~december

0

Number of seconds since the Unix era began

System-dependent, Typ. from -2147483648~2147483647

4.6.4 other date and time functions
1. Calculation of date and time
Copy CodeThe code is as follows:
$oldtime =mktime (0,0,0,9,24,2008);
$newtime =mktime (0,0,0,10,12,2008);
$days = ($newtime-$oldtime)/(24*3600); Calculate the number of days between two time intervals
Echo $days; Output 18
?>

2. Check the date
The Checkdate () function can be used to check whether a date data is valid and has the following syntax:
BOOL Checkdate (int $month, int $day, int $year)
Copy CodeThe code is as follows:
Var_dump (Checkdate (12,31,2000)); output bool (TRUE)
Var_dump (Checkdate (2,29,2001)); output bool (FALSE)
?>

3. Set the time zone
The default is Greenwich Mean time, so it may be different from local time when the current time is displayed. PHP provides a function date_default_timezone_set () that can modify the time zone.
The syntax format is as follows:
BOOL Date_default_timezone_set (string $timezone _identifier)
The parameter $timezone_identifier is the time zone to be specified.
The values available in mainland China are Asia/chongqing,asia/shanghai,asia/urumqi (Chongqing, Shanghai, Urumqi, respectively). Beijing time can use PRC.
4.5 Instance-Generate calendar
Copy CodeThe code is as follows:
$year =@$_get[' year '; Get the year of the address bar
$month =@$_get[' month '; Get the month of the address bar
if (empty ($year))
$year =date ("Y"); Year initialized to year
if (empty ($month))
$month =date ("n"); Initialize the month of the month
$day =date ("J"); Get the number of days of the day
$WD _ar=array ("Day", "one", "two", "three", "four", "five", "six"); Week Array
$WD =date ("W", Mktime (0,0,0, $month, 1, $year)); Calculate the first day of the month is the days of the week
Year link
$y _lnk1= $year <=1970? $year =1970: $year-1; Last year
$y _lnk2= $year >=2037? $year =2037: $year +1; Next year
Monthly links
$m _lnk1= $month <=1? $month = 1: $month-1; Last month
$m _lnk2= $month >=12? $month =12: $month +1; Next month
echo "


















"; Output year, click the < link to skip to the previous year, click the > link to skip to the next year Echo "; Output month, click the < link to skip to the previous month, click the > link to skip to next month Echo "; echo " "; for ($i =0; $i <7; $i + +) {echo " "; Output Week Array} echo " "; $tnum = $wd +date ("T", Mktime (0,0,0, $month, 1, $year)); Calculates the day of the week plus the number of days in the month for ($i =0; $i < $tnum; $i + +) {$date = $i +1-$wd;//calculates the position of the number of days in the table if ($i%7==0) echo " "; The start of the line echo " "; if ($i%7==6) echo " "; End of Line} echo "

< ". $year." Year >

< ". $month." Monthly >
$WD _ar[$i]
";
if ($i >= $wd)
{
if ($date = = $day && $month ==date ("n"))///If the day of the month is black
echo "". $day."";
Else
Echo $date; Number of output days
}
echo "
";
?>

http://www.bkjia.com/PHPjc/323881.html www.bkjia.com true http://www.bkjia.com/PHPjc/323881.html techarticle 1.UNIX timestamp PHPD processing data, especially when formatting the time-type data in the database, the time-type data must first be converted to a UNIX timestamp for processing. Different database ...

  • 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.