PHP dateLOGIN

PHP date

PHP date() function is used to format time/date.

PHP timestamp

UNIX timestamp (timestamp) is a very important concept about time and date in PHP. It represents 00:00 on January 1, 1970: The sum of seconds from 00:00 to the current time.

PHP provides the built-in function time() to obtain the timestamp of the server's current time.

Example:

<?php
  echo time();
?>

After running the above example, you will get a string of numbers similar to this: 1379115455

The typical range of valid timestamps is 1901 Greenwich Mean Time December 13, 20:45:54 to January 19, 2038, 03:14:07 (this range conforms to the minimum and maximum values ​​of 32-bit signed integers). However, starting from 5.1.0, the problem that the year range can only be between 1901 and 2038 has been overcome.

Tips

1. What time() always gets is the current timestamp, so it is an unfixed value

2. If the timestamp is a negative number, it is the value from January 1, 1970 00:00:00 Push forward

Convert date to timestamp

PHP provides functions to easily convert various forms of dates Convert to timestamp, the main functions of this type are:

• strtotime(): Parse the date and time description of any English text into a timestamp.

• mktime(): Get timestamp from date.

The strtotime() function is used to convert the date represented by the English text string into a timestamp. It is the inverse function of date() and returns the timestamp successfully, otherwise it returns FALSE.

Syntax:

int strtotime (string time [, int now])

The parameter time is the parsed string, which is The date according to the GNU date input format.

<?php
  echo strtotime("2009-10-21 16:00:10");        //输出类似 1256112010
  echo strtotime("10 September 2008");          //输出类似 1220976000
  echo strtotime("+1 day"), "<br />";           //输出明天此时的时间戳
?>

mktime() function is used to get the timestamp from the date, and returns the timestamp successfully, otherwise it returns FALSE.

Syntax:

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

Example:

<?php
  echo mktime(21, 50, 55, 07, 14, 2010);   //输出“1279115455”
?>

Parameters can be omitted from right to left. Any omitted parameter will be set to the current value of the local date and time.

mktime() is useful for doing date calculations and validation, it will automatically calculate the correct value for out-of-range input. For example, the following example outputs 2008-01-01:

<?php
 echo date("Y-m-d", mktime(0, 0, 0, 12, 32, 2007));
 echo date("Y-m-d", mktime(0, 0, 0, 13, 1, 2007));
?>

The last day of the next month:

The last day of any given month can be represented It is the "0" day of the next month, not -1 day, as in the following example:

<?php
  $lastday = mktime(0, 0, 0, 3, 0, 2008);
  echo strftime("2008年最后一天是:%d", $lastday);
?>

Browser output:

2008 The last day is: 29

##PHP date() function

PHP date() function can Timestamps are formatted to a more readable date and time.

A timestamp is a character sequence that represents the date/time when a certain event occurred.

Syntax

string date ( string $format [, int $timestamp ] )

##Parameters                                                                                                                                       

##

format Required. Specifies the format of the timestamp.

timestamp Optional. Specify timestamp. The default is the current date and time.

PHP Date() - Format date

The first required parameter format of the date() function 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 formatting:

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

The output of the above code is as follows :

    2016/05/11<br>
      2016.05.11<br>
                     
         

Several parameters: The English of

h is: hour, which represents the hour. The English of i is: minute, which represents the minute. The English of

s is: second, which represents the second.

Write it all:

<?php
  //就可以显示出来当前的时间了哟。
  echo date('Y-m-d H:i:s');
?>

The format string can recognize the string of the following format parameters

format characters                                                  

Return Value examples#日                                                                                                                 2 digits of leading zeros Numbers 01 to 31
D Day of the week, text representation, 3 letters Mon to Sun

j Day of the month, no leading zero 1 to 31 l (lower case letter of "L") Day of the week, complete text format Sunday to Saturday

N ISO-8601 format number represents the day of the week (new in PHP 5.1.0) 1 (meaning Monday) to 7 (meaning Sunday)

S The English suffix after the day of the month, 2 characters st, nd, rd or th. Can be used with j

w The day of the week, the number represents                                                                                                                                                                                                                                      0 to 365

Week --- --- ---

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

month --- ---

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 Months represented by three-letter abbreviations Jan to Dec

n Numbers with no leading zeros 1 to 12

t given The number of days in the month 28 to 31

——————————

L is the leap year. If it is a leap year is 1, otherwise it is 0

## o 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. (Added in PHP 5.1.0) Examples: 1999 or 2003

Y 4-digit year represented by 2 digits                                                                                                                                                                                                                    to

time --- ---

a lowercase morning and afternoon value AM or PM

A MM or PM

# B Swatch Internet standards , 12-hour format, no leading zeros               1 to 12  

G   Hours, 24-hour format, no leading zeros                 0 to 23  

h                                                                                                  01 to 12

H hours, 24 -hour format, there is Zero Zero 00 to 23

i, there is a few minutes from the front guide zero to 59 & gt; 59>

u milliseconds (newly added in PHP 5.2.2). 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

Time zone --- ---

e Time zone identifier (new in PHP 5.1.0) For example: UTC, GMT, Atlantic/Azores

I Whether It is daylight saving time. If it is daylight saving time, it is 1, otherwise it is 0.

O The number of hours difference from Greenwich Mean Time. For example: +0200

P The difference from Greenwich Mean Time (GMT) , there is a colon separating hours and minutes (newly added in PHP 5.1.3) For example: +02:00

T The time zone where this machine is located For example: EST, MDT ([Translator's Note] under Windows Complete text format, 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

Complete date/time --- ---

c ISO 8601 format date (new in PHP 5) 2004-02-12T15:19:21+00:00

r RFC 822 date format For 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( )

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('Y-m-d H:i:s'); ?>
submitReset Code
ChapterCourseware