PHP function to...LOGIN

PHP function to obtain period time information

Before formally learning the date function, you must understand a few concepts:

1. Time zone

2. Universal time

3.Unix timestamp

Time zone

This concept has been heard a lot before. Let's say a few words. The real areas we use in real life are also regulated in computers.
When the International Longitude Conference was held in Washington in 1884, in order to overcome time confusion, the world was divided into 24 time zones.
In China, the time in Dongba District, where the capital Beijing is located, is used as the unified time for the whole country.

Universal Time

Not only astronomers use Greenwich Time (English abbreviation: GMT), but this term also often appears in news newspapers. We know that there are local times everywhere. If we use local time to record a major international event, it will be complicated and inconvenient. And it will be easy to make mistakes as time goes by. Therefore, astronomers proposed a recording method that is acceptable and convenient for everyone, which is to use the local time in Greenwich (a region in the UK) as the standard.

unix timestamp

The computer itself does not know time. We set a time in the computer to facilitate calculations. So we specified a calculation method, Unix timestamp.
The number of seconds elapsed from the Unix epoch (0:00 on January 1, 1970) to a time.

We have learned a few concepts, now we can start to learn the time function.

1. Set the time zone

If we are a multinational multilingual program, we usually write a time zone in the configuration file, each time when the program is running. The time zone settings will be read to display the time.

The function to set the time zone is:
1).date_default_timezone_get()
2).date_default_timezone_set()

We will not focus on the first function, it is relatively simple.

Usage is as follows:

string date_default_timezone_get (void)

Function is as follows:
Get the default value used by all date and time functions in a script Time zone

Example:

<?php
echo date_default_timezone_get ();
?>

This displays the currently set time.

The second function is the key point:

The usage is as follows:

bool date_default_timezone_set (string $timezone_identifier)

The functions are as follows:
Default time zone used for all date and time functions

Example:

<?php

//定义一下时区常量,以后你可以放到配置文件里
define('TIME_ZONE','Asia/shanghai');

//执行函数
date_default_timezone_set(TIME_ZONE);

echo date('Y-m-d H:i:s');

?>

Try comparing the code of the above example, and then comment out date_default_timezone_set and see what will be prompted.

Note:
For the time zone list, please see the official manual http://php.net/manual/zh/timezones.php

2.time() to obtain the current unix timestamp

The function of time() function is to obtain the Unix timestamp of the current time.

The following code outputs the Unix timestamp of the current time.

<?php
   $time=time();
   print_r( $time);
?>

Program running result:
1421597858

3. "Flax drop" is the key to learning time processing in PHP

Y The English word is year, which means year - —亚

m English represents month, which represents the month——hem

d English represents day, which represents the date——fall

So we need to output the year before , month, date. For example: July 1, 1997, we can use the above three parameters.

<?php

echo date('Y年m月d日');
?>

You can try running the code to see if it is displayed.

There are several parameters after:

H:m:s represents: hours, minutes and seconds

h in English is: hour represents hours

The English version of i is: minute, which represents minutes. The English version of s is: second, which represents seconds.

If written in full, it is:

<?php

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

The date function is used to format a time. Output to facilitate time display or storage. The syntax format is as follows:

string date ( string $forrnat [, int $tirnestamp] )

In the parameter list:

$timestamp is a timestamp, and the function presses this timestamp into $format Output in the specified format.

If $timestamp has no input value, it defaults to the current time.

$format is a string of time output format, and the specified characters need to be used to construct the output format.

Format parameter table of date function:

##DDay of the week in English, 3 letters Mon to SunjThe day of the month without leading zeros1 to 31l(letter)Day of the week in EnglishSunday to SaturdayN1 Format number represents the day of the week 1 (meaning Monday) to 7 (meaning Sunday)SThe English suffix after the number of days in each month, 2 characters st, nd, rd or th. Can be used with jgwThe day of the week, the number represents 0 (indicating Sunday) to 6 (indicating Saturday) zDay of the year0 to 366WThe week in the year, each week starts on Monday42 (the 42nd week of the year)Fmonth , complete text format January to DecembermThe number represents the month, with leading zeros01 to 12M3 letter abbreviation for the month Jan to DecnThe number represents the month, without leading zeros1 to 12tThe number of days in a given month28 to 31LWhether it is a leap yearIf it is a leap year, it is 1, otherwise it is ooFormat year numberFor example, 2007Y4 digits represent the complete yearFor example, 1999 or 2008y2-digit year For example, 99 or 08aLowercase AM and PM valuesam or pm##AgGise##USince the Unix epoch Number of seconds so farLong integer number


3. getdate gets the current system time

getdate is used to get the current system time, or get the specific meaning of a timestamp. The timestamp is a long integer, and the syntax format of getdate is as follows.

array getdate ([ int $timestamp = time() ] )

The return value of the function is an array containing time information obtained based on timestamp. If there are no parameters, the current time will be returned. The array returned by getdate, the key name includes complete information of time and date.

CharactersDescriptionReturn value
dThe day of the month, 2 digits with leading zeros01 to 31
Uppercase AM and PM valuesAM or PM
hour, 12-hour format, no leading zeros 1 to 12
Hour, 24-hour format, no leading zeros 0 to 23
Yes Minutes with leading zeros 00 to 59
Seconds with leading zeros 00 to 59
Time zone identifier
## secnodsSecondsNumbers 0 to 59minutesMinutesNumbers 0 to 59hoursHoursNumbers 0 to 23mdayThe first day of the month DaysNumbers 1 to 31wdayDays of the weekNumbers 0 (meaning Sunday) to 6 (Indicates Saturday) monMonthNumber 1 to 12yearYearComplete year represented by 4 digitsydayDay of the yearNumber 0 To 365##weekdaymonth0
Key nameDescriptionReturn value
Day of the week in EnglishSunday to Saturday
Month in EnglishJanuary to December
The number of seconds since the Unix epochLong integer Number


The following code can return the details of the getdate array.

<?php
   $mytime=getdate(); 
   print_r( $mytime);
?>

print_r can output all the key names and values ​​​​in an array. Running this code, the results are as follows. The program outputs the time and date details of the current computer:

Array
(
    [seconds] => 1            //秒
    [minutes] => 10            //分钟
    [hours] => 17            //小时
    [mday] => 18            //日
    [wday] => 0            //星期中的第几天
    [mon] => 1            //月
    [year] => 2015            //年
    [yday] => 17            //年中的第几天
    [weekday] => Sunday        //星期
    [month] => January        //月份
    [0] => 1421597401        //时间戳
)

After understanding the getdate function and the returned array, it is easy to obtain the current time information. The following code uses the getdate function to obtain time information, and calls the value of the returned time array to output the time information.

<?php 
$mytime = getdate();
echo "年 :".$mytime['year']."\n";
echo "月 :".$mytime['mon']."\n";
echo "日 :".$mytime['mday']."\n";
echo "时 :".$mytime['hours']."\n";
echo "分 :".$mytime['minutes']."\n";
echo "秒 :".$mytime['seconds']."\n";
echo "一个小时中的第几钟 :".$mytime['minutes']."\n";
echo "这是一分钟的第几秒 :".$mytime['seconds']."\n";
echo "星期名称 :".$mytime['weekday']."\n";
echo "月份名称 :".$mytime['month']."\n";
echo "时间戳   :".$mytime[0]."\n";
?>

Run this program and the detailed information of the current time will be displayed. The results of running the program are as follows.

Year:2015

Month:1Day:18
Hour:17
Minute:14
Second:11
The first day of the week Day: 14
Day of the year: 11
Name of week: Sunday
Name of month: January
Time stamp: 1421597651

Next Section

<?php $timestamp = strtotime($time); $date = date("y-m-d",$timestamp); echo $date; ?>
submitReset Code
ChapterCourseware