This article mainly introduces the relevant knowledge of the time function date and commonly used time calculations in PHP, which has a good reference value. Let’s take a look at it with the editor.
has been used in projects until today, yesterday, this week, this month, this quarter, this year, last week, last month, last quarter, etc. Timestamps, take advantage of this I have plenty of time recently, so I plan to summarize and study the relevant time knowledge points of PHP
1, read the PHP manual date function
commonly used time Function:
checkdate() verifies whether a time is correct
date_default_timezone_get() obtains the time zone used by the current script
date_default_timezone_set() sets the script The time zone used ini_set() can also be satisfied, or the configuration file can be modified
date_sunrise() date_sunset() returns the sunrise time and sunset time of the given date and location
date() format A date, there will be details below
Getdate() Gets the relevant information of the date and time
Gettimeofday() Gets the relevant information of the current time
idate() Changes the local time The date is formatted as an integer, but only accepts one character as parameter
Microtime() returns the current timestamp and number of seconds
mktime() gets the timestamp of a date
strtotime() parses the date seconds of English text into a timestamp
2, important functions are explained in detail
##date() formats one Date##
string date( string $format [, int $timestamp] ) remembered in int/ʻint $timestamp] / or date , this has leading zeros, such as 01,02
D The day of the week, which is the abbreviation of the English day of the week, Mon to Sun
, this is the complete English format, Sunday to Saturday
N Use numbers to indicate the days of the week, 1 is Monday, 7 is Sunday
S The English suffix
# after the number of days in the month ## w The day of the week, represented by numbers, 0 is Sunday, 6 is Saturday z The day of the year 0 to 365 …………**** lately be in numbers) F Month, complete English word m Month number format, with leading 0 M Three-letter abbreviated month n Number The month represented, without leading 0 t L Detect whether it is a leap year, leap year is 1, month is 0 Y 4 digits The year represented by digits y The year represented by 2 digits a The value of the morning or afternoon in lowercase letters A The value of the morning or afternoon in uppercase letters G 0 i The number of minutes with leading 0 s The number of seconds with leading 0 u The number of milliseconds, the date() function returns the format of 000000 E 22:21 00:00 Time in format U ##The difference with date is that this function can only pass one parameter, date() can pass multiple parameters B Internet time d The day of the month h 12-hour time H 24-hour time i Minutes Returns 1 if daylight saving time is enabled, otherwise 0 L If it is a leap year, return 1, otherwise return 0 Number of seconds since 1970 w Day of the week W Number of week in the year, week starts on Mondayy Year, 1 or 2 Number of digits Y Year 4 digit number
z Day of the year#strtotime() function connection
Usage example
strtotime ("now"); strtotime ("10 September 2017"); strtotime ("+1 day"); strtotime ("+1 week"); strtotime ("+1 week 2 days 4 hours 2 seconds"); strtotime ("next Thursday"); strtotime ("last Monday");
$times = []; function makeTime(){ //获取今日开始时间戳和结束时间戳 $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y')); $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1; $times['today']['begin'] = $beginToday; $times['today']['end'] = $endToday; //获取昨日起始时间戳和结束时间戳 $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y')); $endYesterday=mktime(0,0,0,date('m'),date('d'),date('Y'))-1; $times['yesterday']['begin'] = $beginYesterday; $times['yesterday']['end'] = $endYesterday; //获取上周起始时间戳和结束时间戳 $beginLastweek=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')); $endLastweek=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')); $times['lastWeek']['begin'] = $beginLastweek; $times['lastWeek']['end'] = $endLastweek; //获取本月起始时间戳和结束时间戳 $beginThismonth=mktime(0,0,0,date('m'),1,date('Y')); $endThismonth=mktime(23,59,59,date('m'),date('t'),date('Y')); $times['thisMonth']['begin'] = $beginThismonth; $times['thisMonth']['end'] = $endThismonth; //获取本周开始时间和结束时间,此例中开始时间为周一 $defaultDate = date('Y-m-d'); $first = 1; $w = date('w',strtotime($defaultDate)); $beginWeek = strtotime("$defaultDate-" . ($w?$w-$first:6) . 'days'); $endWeek = $beginWeek + 6*24*3600-1; $times['thisWeek']['begin'] = $beginWeek; $times['thisWeek']['end'] = $endWeek; //获取上月的起始时间戳和结束时间戳 $beginLastmonth=mktime(0,0,0,date('m')-1,1,date('Y')); $endLastmonth=mktime(23,59,59,date('m')-1,date('t'),date('Y')); $times['LastMonth']['begin'] = $beginLastmonth; $times['LastMonth']['end'] = $endLastmonth; //获取今年的起始时间和结束时间 $beginThisyear = mktime(0,0,0,1,1,date('Y')); $endThisyear = mktime(23,59,59,12,31,date('Y')); $times['thisYear']['begin'] = $beginThisyear; $times['thisYear']['end'] = $endThisyear; //获取上年的起始时间和结束时间 $beginLastyear = mktime(0,0,0,1,1,date('Y')-1); $endLastyear = mktime(23,59,59,12,31,date('Y')-1); $times['lastYear']['begin'] = $beginLastyear; $times['lastYear']['end'] = $endLastyear; //获取本季度开始时间和结束时间 $season = ceil((date('n'))/3);//当月是第几季度 $beginThisSeason = mktime(0, 0, 0,$season*3-3+1,1,date('Y')); $endThisSeason = mktime(23,59,59,$season*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y')); $times['thisSeason']['begin'] = $beginThisSeason; $times['thisSeason']['end'] = $endThisSeason; //获取上季度的起始时间和结束时间 $beginLastSeason = mktime(0, 0, 0,($season-1)*3-3+1,1,date('Y')); $endLastSeason = mktime(23,59,59,($season-1)*3,date('t',mktime(0, 0 , 0,$season*3,1,date("Y"))),date('Y')); $times['lastSeason']['begin'] = $beginLastSeason; $times['lastSeason']['end'] = $endLastSeason; return $times; } $times = makeTime();
目前是我之前用到的时间戳,后期还会积累汇总,避免重复造轮子。
The above is the detailed content of Detailed graphic explanation of time function date and commonly used time calculations in PHP. For more information, please follow other related articles on the PHP Chinese website!