Home  >  Article  >  Backend Development  >  Get system time with PHP_PHP tutorial

Get system time with PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:51:311021browse

I saw someone writing it online, but it wasn’t very specific. I’m a newbie, so I seemed a little tired, so I read the PHP manual carefully, and then wrote the following according to a novice’s way of thinking. text.

has the following expression:

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

(Note: Newbies may not understand the following [,int $timestamp]. In fact, this expression means an optional parameter, which means you can fill it in or not. It does not mean that the date function is executed. This parameter is no longer needed, but this parameter has already been assigned a value when it is defined. If a parameter is passed in, the default parameter will be used, such as date(int a, int b=1), where b has already been defined. )

(Note: $format must be added before changing in PHP)

Let’s first talk about the role of the following parameter $timestamp, which represents the timestamp. The typical range of valid timestamps is December 13, 1901 20:45:54 GMT to January 19, 2038 03:14:07 GMT. (This range conforms to the minimum and maximum values ​​of 32-bit signed integers). However, before PHP 5.1 this range was limited to January 1, 1970 to January 19, 2038 on some systems (such as Windows) (I tried it and it was true).

It roughly means the number of seconds since 1970.1.1 + $timestamp, which is your current time. (Insert a word, if it is more about hardware, the microcontroller has an RTC clock, which means that when the power supply is low, the 32768 crystal oscillator is used to start counting, and then the next time the microcontroller is turned on, the recorded number is read and converted into time, which is )

That is to say

         echo date("Y-m-d h:i:s",0); 
?>

will display 1970-01-01 12:00:00.

In the above formula, $format has the following expression

format character description return value example
Day --- ---
d The day of the month, a 2-digit number with leading zeros 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 (lowercase letter "L") Day of the week, complete text format Sunday to Saturday
N The day of the week represented by numbers in ISO-8601 format (newly added 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 (indicating Sunday) to 6 (indicating Saturday)
z The day of the year 0 to 366
Day --- ---
W The week number in the year in ISO-8601 format, each week starts on Monday (newly added in PHP 4.1.0) For example: 42 (the 42nd week of the year)
Month --- ---
F Month, complete text format, such as January or March January to December
m The month represented by the number, with leading zeros 01 to 12
M three-letter abbreviation for the month Jan to Dec
n Month represented by number, no leading zero 1 to 12
t The number of days in a given month 28 to 31
Year --- ---
L Is it a leap year? If it is a leap year, it is 1, otherwise it is 0
o Year numbers in ISO-8601 format. 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. (Newly added in PHP 5.1.0) Examples: 1999 or 2003
Y The year represented by a 4-digit number. For example: 1999 or 2003
y Year represented by 2 digits For example: 99 or 03
Time --- ---
a Lowercase morning and afternoon values ​​am or pm
A Uppercase morning and afternoon values ​​AM or PM
B Swatch Internet standard time 000 to 999
g Hour, 12-hour format, no leading zeros 1 to 12
G hours, 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 00 to 23
i Minutes with leading zeros 00 to 59>
s Number of seconds, with leading zeros 00 to 59>
Time zone --- ---
e Time zone identifier (newly added 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
The difference between P and 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] In complete text format under Windows, such as "Eastern Standard Time", the Chinese version will display "China Standard Time").
Z Time difference offset in seconds. 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 Date in ISO 8601 format (newly added in PHP 5) 2004-02-12T15:19:21+00:00
r RFC 822 format date For example: Thu, 21 Dec 2000 16:01:07 +0200
U Number of seconds since the Unix epoch (January 1 1970 00:00:00 GMT) See time() Example:
// Set the default time zone to use. Available since PHP 5.1
date_default_timezone_set('UTC'); It is very important to set the time zone. The default time zone and Beijing time are 8 hours apart date_default_timezone_set('PRC');//prc is Beijing time


// Output is similar to: Monday
echo date("l");

//Output similar to: Monday 15th of August 2005 03:12:46 PM
echo date('l dS of F Y h:i:s A');

// Output: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));

/* Use constants in format parameters */
// Output is similar: Mon, 15 Aug 2005 15:12:46 UTC
echo date(DATE_RFC822);

// Output is similar: 2000-07-01T00:00:00+00:00
echo date(DATE_ATOM, mktime(0, 0, 0, 7, 1, 2000));
?>


Excerpted from wolinxuebin’s column

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478196.htmlTechArticleI saw someone writing on the Internet, but it was not very specific. I am a newbie, so it seems a bit tired. , so I read the PHP manual carefully, and then followed the ideas of a novice...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn