PHP date and ti...LOGIN

PHP date and time

PHP date() Function used to format date or time

Timestamp, It is usually a sequence of characters that uniquely identifies a certain moment in time

Formatted date: The date() function will return a string generated by the parameter timestamp according to the specified format.

Syntax

date(format, timestamp)

formatRequired, specifies the format of the timestamp. Optional, specifies the timestamp. The default is the current time and date

A timestamp is a sequence of characters that represents the time and date when a specific event occurred


The format string can recognize the following format parameters String

##Parameter Description

timestamp

## Day  d D## jThe day of the month, without leading zeros1 to 31
format character Description##Return value example
--- ---
The day of the month, a 2-digit number with leading zeros 01 to 31
The day of the week, text representation, 3 lettersMon to Sun
##
l (lower case letter of "L") Day of the week, complete text formatSunday to Saturday
 NISO-8601 format number represents the day of the week (newly added in PHP 5.1.0) 1 (meaning Monday) to 7 (meaning Sunday) )
SThe English suffix after the number of days in a month, 2 characters st, nd, rd or th. Can be used with j
wThe day of the week, the number represents 0 (indicating Sunday) to 6 (indicating Saturday)
zThe day of the year0 to 365
Weekday------

 W
ISO-8601 format The week number in the year, each week starts on Monday (newly added in PHP 4.1.0) For example: 42 (the 42nd week of the year)
moon------

 F

#Month, complete text format, such as January or March January to December
m The month represented by numbers, with leading zeros 01 to 12
M The month represented by the three-letter abbreviation Jan to Dec
n The month represented by the number, without leading Zero1 to 12
tThe number of days in a given month28 to 31
ISO-8601 format year number. This is the same value as Y , except that if the ISO week number (W) belongs to the previous or next year, that year is used. (New in PHP 5.1.0) ## Y y
Year------
L Whether it is a leap yearIf it is a leap year, it is 1, otherwise it is 0



# # o


Examples: 1999 or 2003

The year represented by a 4-digit complete numberFor example: 1999 or 2003
The year represented by a 2-digit numberFor example: 99 or 03
000 to 999## g hour, 12 hour format , no leading zeros 1 to 12
Time------
alower case The morning and afternoon values ​​am or pm
## BSwatch Internet Standard Time
##
G hour, 24-hour format, no leading zeros 0 to 23
h Hour, 12-hour format, with leading zeros 01 to 12
H Hour, 24-hour format, with leading zeros Zero00 to 23
iNumber of minutes with leading zeros00 to 59>
s seconds, with leading zeros 00 to 59>
## Time zone## eTime zone identifier (newly added in PHP 5.1.0)For example: UTC, GMT, Atlantic/Azores IWhether it is daylight saving timeIf it is daylight saving time, it is 1, otherwise it is 0 OThe number of hours difference from Greenwich Mean TimeFor example: +0200



u

Milliseconds (New in PHP 5.2.2 add). It should be noted that the date() function always returns 000000 because it only accepts integer parameters, and DateTime::format() only supports milliseconds.



## Example: 654321

------
### and Greenwich Mean Time (GMT), hours and minutes Separated by colons (new in PHP 5.1.3) For example: +02:00 Complete date/time c


## The difference between P



 T



##The time zone where this machine is located

For example: EST, MDT ([Translator's Note] In complete text format under Windows, such as "Eastern Standard Time", the Chinese version will display "China Standard Time").


Z

The number of seconds of the time difference offset. Time zone offsets west of UTC are always negative, and time zone offsets east of UTC are always positive.


-43200 to 43200

-- ----
Date in ISO 8601 format (new in PHP 5)2004-02-12T15 :19:21+00:00
r Date in RFC 822 formatFor example: Thu, 21 Dec 2000 16:01:07 +0200


U

The number of seconds since the Unix epoch (January 1 1970 00:00:00 GMT)


See time()

PHP date() format date

The first required parameter of the date() function, format, specifies how to format the date/time.

Here are some available characters:

d - represents the day of the month (01 - 31)

m - represents the month (01 - 12)

Y - represents the year (four digits)

For a list of all characters available in the format parameter, please consult our PHP Date reference manual, date() function.

You can insert other characters between letters, such as "/", "." or "-", so that you can add additional formats:

Example

<?php
echo date("Y/m/d") . "<br>";
echo date("Y.m.d") . "<br>";
echo date("Y-m-d");
?>

Program running result:

2016/10/09
2016.10.09
2016-10-09


The following characters are commonly used for time:

· h - 12 with leading zero Hour Hour format

· i - minute with leading zero

· s - second with leading zero (00 -59)

· a - lowercase noon and afternoon (am or pm)


Example

The following example outputs the current time in the specified format:

<?php
echo date ("H:i:sa");
?>

The program execution result:

15:53:06pm

The PHP date() function will return the current time/date of the server


Get the time zone

If the time returned from the code is not the correct time, it is possible that your server is located in another country or is set to a different time zone .

So if you need an accurate time based on a specific location, you can set the time zone to use.

The following example sets the time zone to "Asia/Shanghai", and then outputs the current time in the specified format:

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码
date_default_timezone_set("Asia/Shanghai");
echo "当前时间是 " . date("h:i:sa");

?>

The program execution result:

The current time is 03:54:49pm


##Created date via PHP mktime()
## The optional timestamp parameter in the #date() function specifies the timestamp. If you do not specify a timestamp, the current date and time will be used (as in the example above).

The mktime() function returns the Unix timestamp of a date. A Unix timestamp contains the number of seconds between the Unix epoch (January 1, 1970 00:00:00 GMT) and the specified time.

grammar

mktime(hour,minute,second,month,day,year)

The following example A series of parameters in the mktime() function will be used to create the date and time

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码
$d=mktime(9, 12, 31, 6, 10, 2016);
echo "创建日期是 " . date("Y-m-d h:i:sa", $d);
?>

Program running results:

The creation date is 2016-06-10 09:12:31am


Create dates from strings via PHP strtotime()

The PHP strtotime() function is used to convert human Convert the read string to Unix time.

Syntax

##strtotime(time,now)

The following example creates date and time through strtotime() function:

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码
$d=strtotime("10:38pm April 15 2016");
echo "创建日期是 " . date("Y-m-d h:i:sa", $d);
?>

Program running result:

The creation date is 2016-04-15 10:38:00pm


PHP is very smart about converting strings to dates, so you can use a variety of values:

Example

<?php
$d=strtotime("tomorrow");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("next Saturday");
echo date("Y-m-d h:i:sa", $d) . "<br>";
$d=strtotime("+3 Months");
echo date("Y-m-d h:i:sa", $d) . "<br>";
?>

Program running result:

2016-10-10 12 :00:00am

2016-10-15 12:00:00am
2017-01-09 04:01:09pm

Note: strtotime() It's not perfect, so remember to check the strings you put in it.


Example

<?php
$startdate = strtotime("Saturday");
$enddate = strtotime("+5 weeks",$startdate);
while ($startdate < $enddate) {
    echo date("M d", $startdate),"<br>";
    $startdate = strtotime("+1 week", $startdate);
}
?>

Program running result:

Oct 15

Oct 22
Oct 29
Nov 05
Nov 12


## Example
Use the strtotime() function to obtain the timestamp of the English format date and time string

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码
echo strtotime("now"),"<br>";           //当前时间的时间戳
echo "输出时间:".date("Y-m-d H:i:s",strtotime("now")),"<br>";  //输出当前时间
echo strtotime("24 May 2016"),"<br>";                     //输出指点时间的时间戳
echo "输出时间:".date("Y-m-d H:i:s",strtotime("24 May 2016")),"<br>";     //输入指定的日期
?>

Program execution result:

1476000300
Output time: 2016-10-09 16:05:00

1464019200
Output time: 2016-05-24 00:00:00


Example

Realizing the countdown function

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码
$time=strtotime(date("Y-m-d H:i:s"));     //当前的系统时间
$time1=strtotime("2017-10-1  12:00:00");  //国庆节的放假时间
$time2=strtotime("2017-1-1");             //元旦的放假时间
$sub=ceil(($time1-$time)/3600);           //(60秒*60分)/小时
$sub1=ceil(($time2-$time1)/86400);        //(60秒*60分*24小时)/天
echo "离国庆放假时间还有 <strong>$sub</strong> 小时";
echo "<p>";
echo"离元旦放假时间还有 <strong>$sub1</strong> 天";
?>

Program running result:

There are 8564 hours left before the National Day holiday

There are -273 days left before the New Year’s Day holiday

##Complete PHP Date Reference Manual


For a complete reference manual for all date functions, please visit our Complete PHP Date Reference Manual.

This reference manual provides a brief description and application examples of each function!


Next Section
<?php echo date ("H:i:sa"); ?>
submitReset Code
ChapterCourseware
    None