Common PHP Time Function usage Summary

Source: Internet
Author: User
Tags echo date time and date
1. How to set the time zone:

After php5, you have to set the time zone yourself, either modify the settings of the php.ini or modify it in your code.

Setting the time zone in php.ini

Date.timezone = PRC

Set the time zone in your code

1 date_default_timezone_set (' Asia/shanghai ');//' Asia/shanghai ' Asia/Shanghai

2 Date_default_timezone_set (' asia/chongqing ');//Where asia/chongqing ' is ' Asia/Chongqing '

3 Date_default_timezone_set (' PRC ');//Where PRC is the "People's Republic of China"

4 Ini_set (' Date.timezone ', ' etc/gmt-8 ');

5 ini_set (' Date.timezone ', ' PRC ');

6 Ini_set (' Date.timezone ', ' Asia/shanghai ');

7 ini_set (' Date.timezone ', ' asia/chongqing ');

2.php time function and usage:

Checkdate: Verify the correctness of the date.

BOOL Checkdate (int $month , int $day , int $year )

The value of month is from 1 to a Day value that is within the range of days given and the leap year month has been taken into account; The value of year is from 1 to 32767.

Date: Formats the time of the server.

String Date (string $format [, int $timestamp]): Returns timestamp the string that is generated by the integer as a given format string. If no timestamp is given, the local current time is used. In other words, timestamp it is optional and the default value is time ().

Date ("Y-m-d h:i:s", Strtotime ("1 days")//Yesterday at this time, tomorrow is "1 day", the day before yesterday is "2 days";
Date ("Y-m-d h:i:s", Strtotime ("0 Days"))///The time of the present moment, equivalent to date ("Y-m-d h:i:s")

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".

Time: Gets the UNIX timestamp for the current time.

The int time (void) returns the number of seconds since the Unix era (00:00:00 GMT, January 1, 1970) to the current.

$nextWeek = time () + (7 * *);//7 days; Hours; mins; 60secsecho ' Now:       '. Date (' y-m-d h:i:s '). " \ n "; Echo ' Next Week: '. Date (' y-m-d h:i:s ', $nextWeek). " \//Using Strtotime (): Echo ' Next Week: '. Date (' y-m-d h:i:s ', Strtotime (' +1 Week ')). " \ n "; Echo ' Previous Week: '. Date (' y-m-d h:i:s ', Strtotime ('-1 week ')). " \//or with Strtotime ('-7 day ')

Mktime: Gets the UNIX time stamp.

int mktime ([int $hour = Date ("H") [, int $minute = Date ("i") [, int $second = Date ("s") [, int $month = Date ("n") [, int $day = Date ("J") [, int $year = Date ("Y") [, int =-1]]] []]] $is_dst ) The Unix timestamp is returned based on the parameters given. The timestamp is a long integer containing the number of seconds from the Unix era (January 1 1970 00:00:00 GMT) to a given time. If the argument is illegal, this FALSE function returns (returns -1before PHP 5.1).

$lastday = mktime (0, 0, 0, 3, 0, 2000);//The parameters in parentheses indicate the time of day and the month of Echo strftime ("Last Days in the Feb is:%d", $lastday). " <br> "; $lastday = mktime (0, 0, 0, 4, -31, +); Echo strftime (" Last day in Feb:%d ", $lastday);

Output:

Last day in Feb is:29last day in Feb is:29


GETDATE: Get time and date information.

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

$today = getdate ();

Var_dump ($today);

Output:

Array (size=11)  ' seconds ' =>int '  minutes ' =>int, '  hours ' =>int 8  ' Mday ' =>int 4  ' Wday ' =>int 3  ' mon ' =>int one  ' year ' =>int  ' yday ' =>int 307  ' weekday ' =>string ' Wednesday ' (length=9)  ' month ' =>string ' November ' (length=8)  0 =>int 1446597867

Microtime: Gets the one out of 10,000-second value of the UNIX timestamp for the current time.

Mixed Microtime ([bool $get_as_float ]) if the $ get_as_float parameter is given and its value is equivalent to TRUE, the function returns a floating-point number.

function Microtime_float () {    list ($usec, $sec) = Explode ("", Microtime ());    return (float) $usec + (float) $sec);} $time _start = Microtime_float (),//Sleep for a whileusleep (10000000),//usleep-is deferred for a specified number of microseconds, 10000000 is 10 seconds, sleep is delayed by how many seconds $ Time_end = Microtime_float (); $time = $time _end-$time _start;echo "did nothing in $time seconds\n";//did Nothing in 9.9999 949932098 seconds

Strftime: The time of the server is formatted locally. http://php.net/manual/zh/function.strftime.php

Gettimeofday: Get the current time. http://php.net/manual/zh/function.gettimeofday.php

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.

Gmmktime: The GMT time stamp for UNIX is obtained.

3. Supplement:

$_server[' Request_time ' is the timestamp of PHP's built-in start of the current page, and the time ()-$_server[' request_time ' at the end of the current page run is the current page runtime (in seconds):

86,400 seconds a Day

A week 86400*7=604800 seconds


Set_time_limit usage: set_time_limit (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.



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;

--------------------------------------------------------------------------------

$endtime = "2004-09-09 18:10:00"; $d 1=substr ($endtime, 17,2); Seconds $d2=substr ($endtime, 14,2); Sub-$d3=substr ($endtime, 11,2); Time $d4=substr ($endtime, 8,2); Day $d5=substr ($endtime, 5,2); Month $d6=substr ($endtime, 0,4); echo $d 1. '-'. $d 2. '-'. $d 3. '-'. $d 5. '-'. $d 4. '-$d 6. " <br> "; Echo Date (" Y-m-d h:i:s ")." <br> "; $now _t=mktime (Date (" H "), Date (" I "), date (" s "), date (" M "), Date (" D "), Date (" Y ")), echo" Time of the moment ". $now _t." <br> "; $now _s=mktime (" $d 3 "," $d 2 "," $d 1 "," $d 5 "," $d 4 "," $d 6 "); echo" Time at 18:10:00 2004-09-09 ". $now _s." <br> "; $end _ts= ($now _t-$now _s)/60;   Calculates the remaining minutes echo "difference". $end _ts. " Points ";

Output

00-10-18-09-09-20042015-11-04 09:02:08 at this time time14465989282004-09-09 18:10:00 time1094724600 difference of 5864572.1333333 minutes




Common Time function Encapsulation:


/**
* Convert seconds to time (year, day, hour, minute, second)
* @param int $time number of seconds like 86400
* @return bool|string
*/
public static function Sec2time ($time)
{
if (Is_numeric ($time)) {
$value = Array (
"Years" = 0, "days" = 0, "hours" = 0,
"Minutes" = 0, "Seconds" and "0",
);
if ($time >= 31556926) {
$value ["years"] = Floor ($time/31556926);
$time = ($time% 31556926);
}
if ($time >= 86400) {
$value ["days"] = Floor ($time/86400);
$time = ($time% 86400);
}
if ($time >= 3600) {
$value ["hours"] = floor ($time/3600);
$time = ($time% 3600);
}
if ($time >= 60) {
$value ["minutes"] = floor ($time/60);
$time = ($time% 60);
}
$value ["seconds"] = floor ($time);

$t = $value ["Years"]. "Year". $value ["Days"]. "Day". " " . $value ["hours"]. "Hours". $value ["Minutes"]. "Points". $value ["Seconds"]. "Seconds";

return $t;

} else {
return (bool) FALSE;
}

}

/**

* The current date is the first week of the month

* @param int $date y-m-d format time, same as

* @return int

* /

function Weekofmonth ($date) {

$firstOfMonth = Strtotime (Date ("y-m-01", Strtotime ($date)));

Return Intval (Date ("W", $date))-Intval (Date ("W", $firstOfMonth)) + 1;

}

Gets the first and last day of the month on which the specified date is located
function Getthemonth ($date)
{
$firstday = Date ("y-m-01", Strtotime ($date));
$lastday = Date ("y-m-d", Strtotime ("$firstday +1 month-1 Day");
Return Array ($firstday, $lastday);
}

PHP gets the first and last day of the week of the specified date

function GetDays ($date)

{
$lastday = Date (' y-m-d ', Strtotime ("$day Sunday"));
$firstday = Date (' y-m-d ', Strtotime ("$lastday-6 Days"));
Return Array ($firstday, $lastday);
}

/**

* Get pre/Post $step for a specified date (the month ordinal of the month before and after the specified date) the first and last day of the month date

* @param $date
* @param $step
* @return String
*/
function Assigntabmonth ($date, $step)
{
$date = Date ("y-m-d", Strtotime ($step. "Months", Strtotime ($date)));//date of processing (date of the month before and after the Get processed)
$firstday = Date ("y-m-01", Strtotime ($date));
$lastday = Date ("y-m-d", Strtotime ("$firstday +1 month-1 Day");
Return Array ($firstday, $lastday);

}

$date = Date (' Ymd ', Strtotime ($date));

$lastday = Date (' Ymd ', Strtotime ("$date Sunday"));//Sunday

$firstday = Date (' Ymd ', Strtotime ("$lastday-6 Days"));//Mon

$firstday = Date (' Ymd ', strtotime (date (' y-m-01 ', Strtotime ($date))));//The first day of the month on which the specified date is located

$lastday = Date ("Ymd", Strtotime ("$firstday +1 month-1 Day");//The Last date of the month specified

$dayCount = $lastday-$firstday + 1; Interval days


MySQL Date time function http://www.jb51.net/article/23966.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.