php date and time processing continued by jeong aki of

  • 2020-05-09 18:18:25
  • OfStack

1. UNIX timestamp
When processing data, phpd needs to convert the time-type data into UNIX timestamp for processing, especially when formatting the time-type data in the database. Different database systems for time type data
Incompatible with the conversion, you need to convert the time to an UNIX timestamp. In this way, Beijing realized the cross-platform of different database systems.
2. Convert time to timestamp
If you want to convert a date and time expressed as a string to a timestamp, you can use the strtotime() function.
The syntax is as follows:
int strtotime(string $time [, int $now ])
Such as:
 
<?php 
echo strtotime('2009-03-05'); // The output 1236211200 
echo strtotime('2009-03-05 10:24:30'); // The output 1236248670 
echo strtotime("10 September 2000"); // The output 968544000 
?> 

Another function that gets the date's UNIX timestamp is the mktime() function,
The syntax is as follows:
int mktime([int $hour [, int $minute [, int $second [, int $month [, int $day [, int $year]]]]]])
Get the date and time
1. data () function
Is to convert a timestamp into a specific date and time string in the given format.
The syntax is as follows:
string date(string $format [, int $timestamp ])
Description:
$format specifies the format of the converted date and time,
$timestamp is the timestamp that needs to be converted; if omitted, the local current time is used, that is, the default value is the value of the time() function.
The time() function returns the timestamp of the current time
The value of the $format parameter of the date function is shown in the following table.
Table 4.6 format codes supported by the date() function

Word operator

Said Ming

Return value example

d

The number of days in a month has two digits leading to zero

01 ~ 31

D

The days of the week, with three letters

Mon to Sun

j

The day of the month with no leading zero

1 to 31

l

Day of the week, full text format

Sunday ~ Saturday

N

Number of days of the week in ISO-8601 format

1 (Monday 1) ~ 7 (Sunday)

S

The English suffix after the days of a month, represented by two characters

st, nd, rd or th can be used with j1

w

The days of the week are numbered

0 (Sunday) ~ 6 (Saturday)

z

The day of the year

0 ~ 366

W

Week 1 of the year ISO-8601, starting on the first day of the week

For example: 42 (week 42 of the year)

F

Month, complete text format, such as January or March

January ~ December

m

The number represents a month with a leading zero

01 ~ 12

M

A month with three initials

Jan ~ Dec

n

The number represents the month without leading zero

1 ~ 12

t

The number of days due to a given month

28 ~ 31

L

Whether it is a leap year

If it's a leap year, it's 1, otherwise it's 0

o

Year Numbers in ISO-8601 format. This is the same as the value of Y, except that if the number of weeks of ISO (W) belongs to the previous or next year, that year is used

For example, 1999 or 2003

Y

The full four-digit year

For example, 1999 or 2003

y

Two digits for the year

For example, 99 or 03

a

Lowercase am and PM values

am or pm

A

Capitalized morning and afternoon values

AM or PM

B

Swatch Internet standard time

000 ~ 999

g

Hour, 12 hour format, no leading zero

1 ~ 12

G

Hour, 24 hour format, no leading zero

0 ~ 23

h

Hour, 12 hour format, leading zero

01 ~ 12

H

Hour, 24 hour format, leading zero

00 ~ 23

i

The number of minutes with a leading zero

00 ~ 59

s

Number of seconds, leading zero

00 ~ 59

e

Time zone sign

For example: UTC, GMT, Atlantic/Azores

I

Whether it is daylight saving time

If it's daylight saving time, it's 1, otherwise it's 0

O

The number of hours away from GMT

For example: + 0200

P

The difference from Greenwich mean time (GMT) is separated by a colon between the hour and the minute

For example: + 2:00

T

The time zone in which the machine is located

For example: EST, MDT

Z

The number of seconds of 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

Date in RFC 822 format

Thu, 21 Dec 2000 16:01:07 +0200

U

The number of seconds from era UNIX to date

time () function

2. getdate () function
You can get an array of date and time information,
The syntax is as follows:
array getdate([ int $timestamp ])
Note: $timestamp is the timestamp to be converted. If it is not given, use the current time.
The function returns an array of date and time information based on $timestamp, with the keys and values shown in table 4.7

Key name

Said Ming

The value of the example

seconds

The number of seconds

0 ~ 59

minutes

The number of minutes

0 ~ 59

hours

Number of hours

0 ~ 23

mday

The number of days in a month

1 to 31

wday

The number of days of the week

0 (for Sunday) ~ 6 (for Saturday)

mon

The number of months

1 ~ 12

year

The full year represented by four digits

For example, 1999 or 2003

yday

The number of days in a year

0 ~ 365

weekday

Full text representation of the day of the week

Sunday ~ Saturday

month

The full text representation of the month

January ~ December

0

Number of seconds since the beginning of era UNIX

System correlation, typical values range from -2147483648 to 2147483647

4.6.4 other date and time functions
1. Date and time calculation
 
<?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 times  
echo $days; // The output 18 
?> 

2. Check the date
The checkdate() function can be used to check whether 1 date data is valid. The syntax is as follows:
bool checkdate( int $month , int $day , int $year)
 
<?php 
var_dump(checkdate(12,31,2000)); // The output bool(TRUE) 
var_dump(checkdate(2,29,2001)); // The output bool(FALSE) 
?> 

3. Set a time zone
The system defaults to Greenwich mean time, so the display of the current time may differ from the local time. PHP provides the function date_default_timezone_set() for changing time zones,
The syntax is as follows:
bool date_default_timezone_set (string $timezone_identifier)
Parameter $timezone_identifier is the time zone to be specified,
The available values for mainland China are Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (chongqing, Shanghai, urumqi, in that order). PRC can be used in Beijing time.
4.5 instance - generate calendar
 
<?php 
$year=@$_GET['year']; // Gets the year of the address bar  
$month=@$_GET['month']; // Get the month of the address bar  
if(empty($year)) 
$year=date("Y"); // Initializes to the year of the current year  
if(empty($month)) 
$month=date("n"); // Initialize as the month of this month  
$day=date("j"); // Gets the number of days of the day  
$wd_ar=array(" day ","1","2","3","4","5","6"); // Week array  
$wd=date("w",mktime(0,0,0,$month,1,$year)); // Calculation of current month 1 What day is it  
// In the link  
$y_lnk1=$year<=1970?$year=1970:$year-1; // on 1 years  
$y_lnk2=$year>=2037?$year=2037:$year+1; // Under the 1 years  
// On the 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 jumps up 1 Year, click" > "The link jumps down 1 years  
echo "<td colspan=4><a href='EX4_15.php?year=$y_lnk1&month=$month'> 
<</a>".$year." years <a href='EX4_15.php?year=$y_lnk2&month=$month'>></a></td>"; 
// To output the month, click" < "Link to last month, click" > "The link jumps to the 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)); // Calculate the day of the week plus the days of the month  
for($i=0;$i<$tnum;$i++) 
{ 
$date=$i+1-$wd; // Calculate the number of days in the table  
if($i%7==0) echo "<tr align=center>"; //1 The beginning of a line  
echo "<td>"; 
if($i>=$wd) 
{ 
if($date==$day&&$month==date("n")) // If it is the day of the month, the days will be darkened  
echo "<b>".$day."</b>"; 
else 
echo $date; // Output the number of the  
} 
echo "</td> "; 
if($i%7==6) echo "</tr> "; //1 End of the line  
} 
echo "</table>"; 
?> 

Related articles: