Here are some of the time functions in PHP:
(1) Date
Usage: Date (format, [time]);
If there is no time parameter, the current time is used. The format is a string, where the following characters have special meanings:
U is replaced by the number of seconds since a starting time (as if it were January 1, 1970)
Y Replace with 4-bit era name.
Y Replace with 2-bit era name.
F Replace the full English name of the month.
M replaced by the abbreviation of the month.
M replaced by the number of months.
Z is replaced by the number of days since January 1 of the year.
D replaces the number of Rewardcash octopus.
L Replace the day of the week with the full English name.
D Replace the English abbreviation of the day of the week.
W replaces the Day of the Week (number).
H is replaced by the number of hours (24-hour system).
H is replaced by the number of hours (12-hour system).
I replace it with the number of minutes.
S is replaced by the number of seconds.
A is replaced with "AM" or "PM".
A is replaced with "AM" or "PM".
S replaces the sequential numeric suffix, for example: "St", "nd", "rd", "th".
The function returns a format string that has been replaced.
(2) getdate (time)
Returns a hash table with each subscript being:
"Seconds"--number of seconds
"Minutes"--Score
"Hours"--Number of hours
"Mday"--Number of days
"Mon"--Number of months
"Year"--era name
"Yday"-the number of days since January 1
"Weekday"--day of the week, English full name
"Month"--month, full name in English
(3) Gmdate
Similar to date, but the time is first converted to Greenwich standard.
(4) Mktime
Usage:
Mktime (hours, fractions, seconds, month, day, year); Returns a time value that can be used for other functions.
(5) Time ()
Usage:
Time (); Returns the number of seconds since zero January 1, 1970.
(6) Microtime ()
Usage:
Microtime (); Returns a string separated into two parts, followed by time ()
The first part of the return value is the number of microseconds.
(7) Checkdate
Checkdate (month, day, year); Returns logical TRUE or logical false. If:
The year between 1900 and 32767 (including 1900 and 32767);
The month is between 1 and 12;
Day within the allowable days of the month (considering leap years);
Returns the logical true.
(8) Set_time_limit
Usage:
Set_time_limit (number of seconds);
Specifies that the program must run at the end of the specified number of seconds from the time the sentence is run, and the program exits with an error.
1 · By
All functions in PHP are in the Unix era, starting January 1, 1970. The date is the number of seconds from this time onwards. When a function call is counted as the number of seconds from this time, it is treated as a (timestamp) timestamp.
local time function
1. String Date (string Format,inieger timestamp)
The function returns a string representing the time, which is controlled by the string format.
Such as:
<?
Print (Date ("Y year M D Day");//output current, month date.
Print (Date ("Y year M month D Day", 60*60*24*365*10);//Output January 1, 1980.
?>
You might ask, how come there's no timestamp? If the timestamp is empty, or does not write, it means to use the current time moment timestamp.
The control that represents the year: Y---Four-bit year y---two-bit year
The controller that represents the month: M---month name from the month of 1-12 F---English month name m---abbreviated
The control that represents the day number: D---preceded by 0 of the month in the date j--before the day number 0
The week's control: l--English week d--abbreviated week
The control for the hour: h--from 1-12 hours h---from 0 to 23 hours
Represents the last afternoon's control of a---am or pm a---am or pm
The control that represents the minute: I---value 00-59
Another z--date of the year
2. Array getdate (integer timestamp)
The function returns a matrix.
Such as:
<?
$current _date=getdate ();
Print ($current _date ("hours"));
Print ($current _date ("minutes");
Print ($current _date ("seconds");
?>
Description
Element description
Hours hours in 24-hour format
Mday date in month
Minutes min
Mon digital form of month
Month Full Name
Seconds number of seconds
Wday number of weeks from 0 to 6
Weekday name of the week
Year years
0 Timestamp is the number of seconds from January 1, 1970 to the present
Yday date of the year in digital form
3. Boolean checkdate (integer month,integer day,integer year)
The function checks if the date is legal. For example:
<?
if (Checkdate (2,29,1980))
Print ("date valid!n");
?>
4. Integer time ()
The function obtains the current timestamp. such as:
<?
Print (Time ());//output a large string of integers
?>
5. Integer mktime (integer hour,integer minutes,integer seconds,integer month, integer day,integer year)
The function returns the timestamp of the given date, which is the number of seconds from January 1, 1970 to the present.
If a parameter goes out of scope, the function can also interpret it, as 1 March is the January of the second year.
Such as:
<?
$currenthour =date ("H");
Print ("50 hours later:");
Print (Date ("H:i A L F ds,y", Mktime ($currenthour +50)));
Print ("<br>n");
?>
6. String Microtime ()
The function returns a string that is the number of milliseconds from the current time + space + seconds since 1970
<?
Print ("Start:microtime () <br>n");
for ($index =0; $index <1000; $index + +)
Print ("good!");
Print ("Stop:microtime () <br>n");
?>
Also, each Linwei standard Time function, not commonly used I will not say! That's all that's used.
--------------------------------------------------------------------------------
2 · An example of a problem
$nowdate = "1999-08-05";
$aa =getdate ($nowdate);
$year = $aa [' Year '];
$month = $aa [' mon '];
echo $year. " </br> ";
Echo $month;
Why get:
1970
1
I want to get:
1999
8
How to solve?
--------------------------------------------------------------------------------
$nowdate = "1999-08-05";
$aa =strtotime ($nowdate);
$year =date ("Y", $AA);
$month =date ("n", $AA);
echo $year. " </br> ";
Echo $month;
--------------------------------------------------------------------------------
3 ·
1, year-month-day
echo Date (' y-m-j ');
2007-02-6
echo Date (' y-n-j ');
07-2-6
Uppercase Y represents the year four digits, while lowercase y represents the two digits of the year;
Lowercase m represents the number of months (with leading), while lowercase n indicates no leading month number.
echo Date (' y-m-j ');
2007-feb-6
echo Date (' y-m-d ');
2007-02-06
The uppercase M represents the 3 abbreviated characters of the month, while the lowercase m represents the number of the month (with leading 0);
J with no uppercase, only lowercase J indicates the date of the month, no leading o, or lowercase D if a month with a leading is required.
echo Date (' y-m-j ');
2007-feb-6
echo Date (' Y-f-js ');
2007-february-6th
Uppercase M represents the 3 abbreviated characters of the month, while uppercase F indicates the full written English of the month. (No lowercase f)
Uppercase S represents the suffix of the date, such as "St", "nd", "rd", and "th", depending on the date number.
Summary:
Indicates that the year can be in uppercase Y and lowercase y;
Indicates that the month can be in uppercase F, uppercase M, lowercase m, and lowercase n (two ways of representing characters and numbers, respectively);
Indicates that the day can be in lowercase d and lowercase j, and uppercase s represents the suffix of the date.
2, Hours: minutes: seconds
By default, the PHP interpretation is displayed as "Greenwich Mean Time", which differs from our local time by 8 hours.
echo Date (' g:i:s a ');
5:56:57 am
echo Date (' H:i:s A ');
05:56:57 AM
The lowercase g represents a 12-hour system without a leading 0, while the lowercase h indicates a 12-hour system with a leading 0.
When using the 12-hour system, it is necessary to indicate that in the afternoon, lowercase a represents the lowercase "am" and "PM", and uppercase a denotes uppercase "AM" and "PM".
echo Date (' g:i:s ');
14:02:26
Uppercase G denotes 24-hour hours, but without preamble, and uppercase H for leading 24-hour hours
Summary:
The letter g means the hour without leading, the letter H denotes the hour with the preamble;
lowercase g, h for 12-hour system, uppercase G, h for 24-hour system.
3, Leap year, week, day
echo Date (' L ');
Leap Year: 0
echo Date (' L ');
Today is: Tuesday
echo Date (' D ');
Today is: Tue
Uppercase L Indicates whether this year is a leap years, a Boolean value that returns 1 for true, otherwise 0;
Lowercase L Indicates the day of the week in English full write (Tuesday);
Instead, use uppercase D to denote the 3-character abbreviation (TUE) of the day of the week.
echo Date (' W ');
Today's Week: 2
echo Date (' W ');
This week is the No. 06 week of the year.
The lowercase w represents the day of the week, and the numeric representation
Uppercase W indicates the number of weeks in a year
echo Date (' t ');
This month is 28 days
echo Date (' Z ');
Today is the 36th day of the year.
Lowercase t indicates the current month and how many days
Lowercase z indicates that today is the first day of the year
4, other
echo Date (' T ');
Utc
Capital T indicates the server's time zone setting
echo Date (' I ');
0
Uppercase I indicates whether the current is daylight saving time, returns 1 for true, otherwise 0
echo Date (' U ');
1170769424
The capital U represents the total number of seconds from January 1, 1970 to the present, which is the UNIX timestamp for the Unix time era.
echo Date (' C ');
2007-02-06t14:24:43+00:00
Lowercase C represents the ISO8601 date, the date format is YYYY-MM-DD, the letter T is used to interval the date and time, the time format is HH:MM:SS, and the time zone is represented by the deviation of Greenwich Mean Time (GMT).
echo Date (' R ');
Tue, Feb 2007 14:25:52 +0000
The lowercase R represents the RFC822 date.
--------------------------------------------------------------------------------
4 ·
Calculating the time difference in PHP is sometimes a hassle!
However, I believe that any language, as long as the grasp of the rules of the rate can find a way, is absolutely decadent and magical.
The work is often to date calculation, the following summarizes several work summarized experience. Write it down lest you forget it later!
1. The first calculation of the time period difference (can be minutes, seconds, days)
$endtime = "2004-09-09 18:10:00";
$d 1=substr ($endtime, 17,2); Seconds
$d 2=substr ($endtime, 14,2); Score of
$d 3=substr ($endtime, 11,2); When
$d 4=substr ($endtime, 8,2); Day
$d 5=substr ($endtime, 5,2); Month
$d 6=substr ($endtime, 0,4); Years
echo $d 1. ' -'. $d 2. ' -'. $d 3. ' -'. $d 5. ' -'. $d 4. ' -'. $d 6. ' n ";
echo Date ("Y-m-d h:i:s"). " n ";
$now _t=mktime (Date ("H"), Date ("I"), date ("s"), date ("M"), Date ("D"), Date ("Y"));
echo $now _t. " n ";
$now _s=mktime ("$d 3", "$d 2", "$d 1", "$d 5", "$d 4", "$d 6");
echo $now _s. " n ";
$end _ts= ($now _s-$now _t)/60; Calculate the remaining minutes
echo $end _ts;
?>
Note $startdate =mktime ("0", "0", "0", "1", "1", "2000");
The resulting value is the total number of seconds from 1970-1-1 to the parameter time so the minute/60 o'clock/3,600 days/3600/24 can be!
If the parameter in Mktime () is defaulted, that means the current date is used.
2. If you have a database, it's easy! If MSSQL can use a trigger! Use a function DateDiff () that calculates the date difference.
If MySQL is the result of the calculation of the difference between the two date fields, the results are saved in another numeric field! Time to call!
--------------------------------------------------------------------------------
5 ·
The following methods for collecting date calculations:
A date plus a few days:
$tdate =date ("y-m-d");
$a =date ("y-m-d", Mktime (0,0,0,date ("M"), Date ("D") +3,date ("Y")));
$sql = "SELECT * from Memo where username= ' $session _user_id '
and compid= $compid and telldate>= ' $tdate ' and telldate<= ' $a '
ORDER BY id DESC ";
$ret =mysql_query ($sql, $conn);
$a is today + three days!
$b =date ("y-m-d", Mktime (0,0,0,date ("M"), Date ("D") -3,date ("Y")));
$b is today-three days!
////
Date plus n days after a date
$days =abs ((Strtotime ("y-m-d")-strtotime ("xxxx-xx-xx"))/86400);
/////
<? Php$date_1= "2003-7-15";//may also be: $Date _1= "2003-7-15 23:29:14"; $Date _2= "1982-10-1"; $d 1=strtotime ($Date _1); $d 2 = Strtotime ($Date _2); $Days =round (($d 1-$d 2)/3600/24); Echo "I have struggled $Days days ^_^"; >
--------------------------------------------------------------------------------
Several methods of calculating time difference in 6 PHP by: Landlord
A simple example is calculating the number of days to borrow, which requires PHP to calculate on a daily basis, and here are a few ways to implement this date calculation:
(1) If you have a database, it's easy! If MSSQL can use a trigger! Use a function DateDiff () that calculates the date difference. If it's MySQL, it will be saved in another numeric field with the calculation of the difference between the two date fields! Time to call!
(2) If you don't have a database, you have to use PHP's time-date function completely! The following are the main explanations:
Example: Calculate the number of days from May 3, 1998 to 1999-6-5:
<? $startdate =mktime ("0", "0", "0", "5", "3", "1998");
$enddate =mktime ("0", "0", "0", "6", "5", "1999");
The resulting value is the total number of seconds from 1970-1-1 to the parameter time: is an integer. So
The following code is so much more:
$days =round (($enddate-$startdate)/3600/24);
Days for the day to get;
If the parameter in Mktime () is the default, it means that the current date is used, so that the number of days from the borrowing date is calculated.
--------------------------------------------------------------------------------
7 ·
Calculating the time difference in PHP is sometimes a hassle!
However, I believe that any language, as long as the grasp of the rules of the rate can find a way, is absolutely decadent and magical.
The work is often to date calculation, the following summarizes several work summarized experience. Write it down lest you forget it later!
1. The first calculation of the time period difference (can be minutes, seconds, days)
$endtime = "2004-09-09 18:10:00";
$d 1=substr ($endtime, 17,2); Seconds
$d 2=substr ($endtime, 14,2); Score of
$d 3=substr ($endtime, 11,2); When
$d 4=substr ($endtime, 8,2); Day
$d 5=substr ($endtime, 5,2); Month
$d 6=substr ($endtime, 0,4); Years
echo $d 1. ' -'. $d 2. ' -'. $d 3. ' -'. $d 5. ' -'. $d 4. ' -'. $d 6. ' n ";
echo Date ("Y-m-d h:i:s"). " n ";
$now _t=mktime (Date ("H"), Date ("I"), date ("s"), date ("M"), Date ("D"), Date ("Y"));
echo $now _t. " n ";
$now _s=mktime ("$d 3", "$d 2", "$d 1", "$d 5", "$d 4", "$d 6");
echo $now _s. " n ";
$end _ts= ($now _s-$now _t)/60; Calculate the remaining minutes
echo $end _ts;
?>
Note $startdate =mktime ("0", "0", "0", "1", "1", "2000");
The resulting value is the total number of seconds from 1970-1-1 to the parameter time so the minute/60 o'clock/3,600 days/3600/24 can be!
If the parameter in Mktime () is defaulted, that means the current date is used.
2. If you have a database, it's easy! If MSSQL can use a trigger! Use a function DateDiff () that calculates the date difference.
If MySQL is the result of the calculation of the difference between the two date fields, the results are saved in another numeric field! Time to call!
--------------------------------------------------------------------------------
8 ·
Date and Time function library
--------------------------------------------------------------------------------
There are 12 functions in this library
Checkdate: Verify the correctness of the date.
Date: Formats the time of the server.
Strftime: The time of the server is formatted locally.
GETDATE: Get time and date information.
Gettimeofday: Get the current time.
Gmdate: Gets the current time after the GMT difference.
Easter_date: Calculates the Easter date.
Easter_days: Calculates the number of dates between Easter and March 21.
Mktime: Gets the UNIX time stamp.
Gmmktime: The GMT time stamp for UNIX is obtained.
Time: Gets the UNIX timestamp for the current time.
Microtime: Gets the one out of 10,000-second value of the UNIX timestamp for the current time.
Function: Checkdate ()
--------------------------------------------------------------------------------
Date and Time function library
Checkdate
Verify that the date is correct.
Syntax: int checkdate (int month, int day, int);
return value: Integer
Function type: Time and date
Content Description
Returns True if the date is valid, or False if there is a problem with the date. This function can be used to check if a date is valid. The valid range is as follows:
The year is 0-32,767 years
Month is 1-12 months
The day changes with the month and leap year
Function: Date ()
--------------------------------------------------------------------------------
Date and Time function library
Date
Formats the time of the server.
Syntax: string date (string format, int [timestamp]);
return value: String
Function type: Time and date
Content Description
The string of the return value is determined by the configured format. If there is an incoming timestamp value, the timestamp is formatted and returned if no time stamp value is passed, and the current server's time is formatted. To convert a date to another language family format, use setlocale () and strftime () two functions. The options for string formatting are as follows:
A-"AM" or "PM"
A-"AM" or "PM"
D-A few days, two digits, if less than two is the front 0; such as: "01" to "31"
D-Days of the week, three English letters; such as: "Fri"
F-month, full name in English; such as: "January"
H-12 hours of the hour; such as: "01" to "12"
H-hour of 24-hour system; such as: "00" to "23"
G-12 hours of the hour, less than two digits do not fill 0; such as: "1" to 12 "
G-24 hours of the hour, less than two does not fill 0; such as: "0" to "23"
I-minute; such as: "00" to "59"
J-A few days, two digits, if less than two bits do not fill 0; such as: "1" to "31"
L-Day of the week, English full name; such as: "Friday"
M-month, two digits, if less than two digits in front of 0; such as: "01" to "12"
N-month, two digits, if less than two digits does not fill 0; such as: "1" to "12"
M-month, three letters of English; such as: "Jan"
S-second; such as: "00" to "59"
S-End With English ordinal number, two English letters; such as: "th", "nd"
T-the number of days in the specified month; such as: "28" to "31"
U-Total number of seconds
W-Number of days of the week, such as: "0" (Sunday) to "6" (Saturday)
Y-year, four digits; such as: "1999"
Y-year, two digits; such as: "99"
Z-the day ordinal of a year; such as: "0" to "365"
Other characters that are not listed above directly list the character.
Usage examples
Example one:
<?
Print (Date ("L DS of F Y h:i:s A"));
Print ("July 1, is on a". Date ("L", Mktime (0,0,0,7,1,2000)));
?>
Example two:
<?
$tomorrow = Mktime (0,0,0,date ("M"), Date ("D") +1,date ("Y"));
$lastmonth = Mktime (0,0,0,date ("M") -1,date ("D"), Date ("Y"));
$nextyear = Mktime (0,0,0,date ("M"), Date ("D", Date ("Y") +1);
?>
Function: Strftime ()
--------------------------------------------------------------------------------
Date and Time function library
Strftime
The time of the server is formatted locally.
Syntax: string strftime (string format, int [timestamp]);
return value: String
Function type: Time and date
Content Description
The string of the return value is determined by the configured format. If there is an incoming timestamp value, the timestamp is formatted and returned if no time stamp value is passed, and the current server's time is formatted locally. The month or week name varies with the local language configuration setlocale ().
The returned string can be in the following format:
%a abbreviation of the week.
%A the full name of the week.
The abbreviation for the%b month name.
The full name of the%B month name.
The%c local end datetime is a better representation of the string.
%d numbers indicate the day ordinal of the month (range 00 to 31).
The%H uses a 24-hour number to denote the number of hours (ranging from 00 to 23).
%I A 12-hour number indicates the number of hours (range 01 to 12).
%j indicates the day of the Year in Numbers (range 001 to 366).
Number of%m months (range from 1 to 12).
%M minutes.
%p indicates the local time with ' AM ' or ' PM '.
%s Number of seconds.
The number of%u is expressed as the week of the current year, and the first one weeks start in Sunday.
The%W number is expressed as the week of the current year, and the first one weeks start in Monday.
%w numbers indicate the day of the week (0 is Sunday).
%x does not include date notation for time.
%x does not include a time representation of the date.
The%y two digits represent the year (range from 00 to 99).
%Y a full year number representation, that is, four digits.
%Z time zone or name abbreviation.
Percent% character.
Usage examples
<?php
SetLocale ("Lc_time", "C");
Print (Strftime ("%A in Finnish is");
SetLocale ("Lc_time", "fi");
Print (Strftime ("%A, in French"));
SetLocale ("Lc_time", "fr");
Print (Strftime ("%A and in German"));
SetLocale ("Lc_time", "de");
Print (Strftime ("%A.
"));
?>
Function: getdate ()
--------------------------------------------------------------------------------
Date and Time function library
GetDate
Get time and date information.
Syntax: array getdate (int timestamp);
return value: Array
Function type: Time and date
Content Description
The elements of the returned array include the following items:
"Seconds"-seconds
"Minutes"-min
"Hours"-when
"Mday"-the day ordinal of the month
"Wday"-the number of days of the week
"Mon"-month number
"Year"-years, number
"Yday"-the number of days of the year; such as: "299"
"Weekday"-the full name of the week; such as: "Friday"
"Month"-the full name of the month; such as: "January"
--------------------------------------------------------------------------------
Function: Gettimeofday ()
--------------------------------------------------------------------------------
Date and Time function library
Gettimeofday
Get the current time.
Syntax: array gettimeofday (void)
return value: Array
Function type: Time and date
Content Description
The elements of the returned array include the following items:
"SEC"-sec
"USec"-one out of 10,000 seconds
"Minuteswest"-minutes of Greenwich Mean
"Dsttime"-the purpose of the time zone
Function: Gmdate ()
--------------------------------------------------------------------------------
Date and Time function library
Gmdate
Get the current time after the GMT difference.
Syntax: string gmdate (string format, int timestamp);
return value: String
Function type: Time and date
Content Description
This function is similar to the date () function, and the difference is that the function returns the time after the GMT time difference (Greenwich Mean, GMT).
Usage examples
<?php
echo Date ("M D Y h:i:s", Mktime (0,0,0,1,1,1998));
Echo gmdate ("M D Y h:i:s", Mktime (0,0,0,1,1,1998));
?>
If the machine executing this example is in Finland (Finland, GMT +0200), the result returned is:
Jan 01 1998 00:00:00
Dec 31 1997 22:00:00
Function: Easter_date ()
--------------------------------------------------------------------------------
Date and Time function library
Easter_date
Calculates the Easter date.
Syntax: int easter_date (int [year]);
return value: Integer
Function type: Time and date
Content Description
Enter a year, the date of the year is returned in the format of the UNIX timestamp, and if no year is entered, the current year is calculated. The value is noted that the input year must be between A.D. 1970-2037, otherwise it cannot be calculated.
Usage examples
<?
echo Date ("M-d-y", Easter_date (1999));
echo Date ("M-d-y", Easter_date (2000));
echo Date ("M-d-y", Easter_date (2001));
?>
Returns the result as
apr-04-1999
apr-23-2000
apr-15-2001
--------------------------------------------------------------------------------
9 ·
Function: Easter_days ()
--------------------------------------------------------------------------------
Date and Time function library
Easter_days
Calculates the number of dates between Easter and March 21.
Syntax: int easter_days (int [year]);
return value: Integer
Function type: Time and date
Content Description
Enter a year to calculate the number of dates between Easter and March 21 in that year, and if no year is entered, the current year. This function can be used instead of easter_date () 1970-2037 years outside the scope of the problem cannot be calculated.
Usage examples
<?
Echo Easter_days (1999);
echo easter_days (1492);
Echo Easter_days (1913);
?>
The returned result is:
14 (4/4)
32 (4/22)
2 (3/23)
--------------------------------------------------------------------------------
10 ·
Function: Mktime ()
--------------------------------------------------------------------------------
Date and Time function library
Mktime
Gets the UNIX time stamp.
Syntax: int mktime (int hour, int minute, int second, int month, int day, int year);
return value: Integer
Function type: Time and date
Content Description
Enter a time that returns a long integer of the UNIX timestamp.
Usage examples
<?
echo Date ("M-d-y", Mktime (0,0,0,12,32,1997));
echo Date ("M-d-y", Mktime (0,0,0,13,1,1997));
echo Date ("M-d-y", Mktime (0,0,0,1,1,1998));
?>
11 ·
Function: Gmmktime ()
--------------------------------------------------------------------------------
Date and Time function library
Gmmktime
Gets the time stamp of the UNIX GMT.
Syntax: int gmmktime (int hour, int minute, int second, int month, int day, int year);
return value: Integer
Function type: Time and date
Content Description
Enter a time that returns the long integer of the UNIX GMT-stamp.
--------------------------------------------------------------------------------
12 ·
Function: Time ()
--------------------------------------------------------------------------------
Date and Time function library
Time
Gets the UNIX time stamp for the current time.
Syntax: int time (void);
return value: Integer
Function type: Time and date
Content Description
Returns the stamp value for the current time.
--------------------------------------------------------------------------------
13 ·
Function: Microtime ()
--------------------------------------------------------------------------------
Date and Time function library
Microtime
Gets the one out of 10,000-second value of the UNIX timestamp for the current time.
Syntax: string microtime (void);
return value: String
Function type: Time and date
Content Description
PHP Time Function collation