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

Source: Internet
Author: User
Tags date format bool empty expression getdate iso 8601 iso 8601 format
1.UNIX time Stamp
PHPD processing of 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 processing. Data for time types in different database systems
cannot be compatible with transformations, then you need to turn time into a Unix timestamp. So Beijing realizes the cross-platform nature of different database systems.
2. Time is converted into time stamp
You can use the Strtotime () function if you want to convert the date and time of the string expression 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:
<?php
echo strtotime (' 2009-03-05 '); Output 1236211200
echo strtotime (' 2009-03-05 10:24:30 '); Output 1236248670
Echo Strtotime ("September 2000"); Output 968544000
?>

Another Unix time stamp function that gets the date 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 Date and Time
1.data () function
is to convert the timestamp to a specific date and time string according to the given format.
The syntax format is as follows:
String Date (string $format [, int $timestamp])
Description
$FORMAT Specifies the format of the converted date and time,
$timestamp is a timestamp that needs to be converted, and if omitted, the local current time is used, the value of the default value of the time () function.
Time () function returns the timestamp of the current time
The $format parameter of the DATE function is taken as the following table.
Table 4.6 Format code supported by the date () function

Characters

Description

Return value Example

D

The first day of the month with a leading zero of 2 digits

01~31

D

The day ordinal of the week, expressed in 3 letters

Mon to Sun

J

Days of the month, no leading zeros

1~31

L

Day of the week, complete text format

Sunday~saturday

N

Day of the week in the ISO-8601 format number representation

1 (Monday) ~7 (Sunday)

S

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

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

W

The day ordinal of the week, the number indicates

0 (Sunday) ~6 (Saturday)

Z

Day ordinal of the year

0~366

W

The first weeks of the ISO-8601 format year, starting from Monday

For example: 42 (42nd Week of the year)

F

month, complete text format, such as January or March

January~december

M

The month that the number represents, with a leading zero

01~12

M

A three-letter-abbreviated month

Jan~dec

N

The month that the number represents, without a leading zero

1~12

T

The number of days that a given month should be

28~31

L

Whether it is a leap year

If it is a leap year is 1, otherwise 0

O

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

For example: 1999 or 2003

Y

Year with 4 digits full representation

For example: 1999 or 2003

Y

Year in 2-digit digits

For example: 99 or 03

A

Lowercase morning and afternoon values

AM or PM

A

Uppercase morning and afternoon values

AM or PM

B

When Swatch Internet standards

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

For example: Utc,gmt,atlantic/azores

I

is Daylight saving time

If daylight saving time is 1, or 0

O

Hours of difference from GMT

For example: +0200

P

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

For example: +02:00

T

Time zone in which this machine resides

For example: EST,MDT

Z

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

-43200~43200

C

Date in ISO 8601 format

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

R

RFC 822 date in format

Thu, Dec 2000 16:01:07 +0200

U

Number of seconds since the start 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 be converted, and the current time is used if not given.
The function returns an array of date and time information based on $timestamp, and the array's key names and values are shown in table 4.7

Key Name

Description

Examples of values

Seconds

The number of seconds indicates

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 (Sunday) ~6 (expression Saturday)

Mon

The number of months indicates

1~12

Year

Full year with 4-digit digits

For 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 start of the Unix era

System-related, typical value from -2147483648~2147483647

4.6.4 other date and time functions
1. Calculation of date and time
Copy CodeThe code is as follows:
<?php
$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 difference
Echo $days; Output 18
?>

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

3. Set time zone
The system defaults to Greenwich Mean time, so it may be different from the local time when the current time is displayed. PHP provides a function to modify the time zone Date_default_timezone_set (),
The syntax format is as follows:
BOOL Date_default_timezone_set (string $timezone _identifier)
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). PRC can be used in Beijing time.
4.5 Instance-Generate calendar
Copy CodeThe code is as follows:
<?php
$year =@$_get[' year ']; The year that gets the address bar
$month =@$_get[' month ']; Get the month of the address bar
if (empty ($year))
$year =date ("Y"); Year initialized to this year
if (empty ($month))
$month =date ("n"); Month initialized to month
$day =date ("J"); Get Days of 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 week
Year link
$y _lnk1= $year <=1970 $year =1970: $year-1; Last year
$y _lnk2= $year >=2037 $year =2037: $year +1; Next year
Month Link
$m _lnk1= $month <=1? $month = 1: $month-1; Last month
$m _lnk2= $month >=12 $month =12: $month +1; Next month
echo "<table cellpadding=6 cellspacing=0 width=200 bgcolor= #eeeeee ><tr align=center bgcolor= #cccccc >";
Output year, click the < link to skip to the previous year, click the > link to skip to the next year
echo "<td colspan=4><a href= ' ex4_15.php?year= $y _lnk1&month= $month ' >
<</a> ". $year." Year <a href= ' ex4_15.php?year= $y _lnk2&month= $month ' >></a></td> ';
Output month, click the < link to skip to last month, click the > link to skip to next month
echo "<td colspan=3><a href= ' ex4_15.php?year= $year &month= $m _lnk1 ' >
<</a> ". $month." Month <a href= ' ex4_15.php?year= $year &month= $m _lnk2 ' >></a></td> </tr> ';
echo "<tr align=center>";
For ($i =0 $i <7; $i + +)
{
echo "<td> $wd _ar[$i]</td>"; Output Week Array
}
echo "</tr>";
$tnum = $wd +date ("T", Mktime (0,0,0, $month, 1, $year)); Count days of the week plus month
for ($i =0; $i < $tnum; $i + +)
{
$date = $i +1-$wd; Calculate the position of days in a table
if ($i%7==0) echo "<tr align=center>"; The beginning of a line
echo "<td>";
if ($i >= $wd)
{
if ($date = = $day && $month ==date ("n"))///
echo "<b>". $day. " </b> ";
Else
Echo $date; Number of days of output
}
echo "</td>";
if ($i%7==6) echo "</tr>"; End of Line
}
echo "</table>";
?>


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.