Chapter 1. Unix Timestamps

Unix timestamp:

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

Correlation function:

Time () – The function returns a timestamp of the current system

Mktime () – gets a Unix timestamp for a date

Format: int mktime(hour [, minute [, second [, month [, day [, year [,is_dst]]]]]]);

Note: the is_dst parameter indicates whether daylight saving time is used. This parameter has been deprecated since PHP5.10.

Strtotime () – resolves the date-time description of any English text into a Unix timestamp

Format: int strtotime (stringnow ] )

Case study:

// System timestamp
echo "Current system timestamp:".time(); / / the number of seconds

//mktime -- give me a year, month, day, hour, minute, second, and I'll give you back a timestamp, very little use
echo "Get the timestamp of the specified date 2033-2-12:".mktime(0.0.0.2.12.2033);

//strtotime() -- use this if used
echo "Get the time stamp of the specified date 2034-1-1:".strtotime('2034-1-1');
echo "Get time stamp from 3 days ago:".strtotime('-3 day');
echo "Get time stamp 3 days later:".strtotime('3 day');
Copy the code

Chapter 2: Getting dates and times in PHP

Getdate – Gets date/time information.

Array getDate ([int timestamp])

Returns an associative array containing date information based on TIMESTAMP. If no timestamp is given, the current local time is considered.

// getDate -- gets the date/time information

$date = getdate();// Current date information
//$date = getdate(234456768); // Get the date information for the specified timestamp

echo "<pre>";// Format output
print_r($date);// Prints the time array

Copy the code

Date – Formats a local time/date.

Format: String format [, int timestamp]

Returns a string generated by timestamp of the integer as the given format string. If no timestamp is given, the local current time is used. In other words, timestamp is optional and the default is time() (the current timestamp).

For example, echo date(” Y year M month d day H: I :s “); // October 28, 2010, 14:22:28

Common parameters:

**Y: four-digit year M: month 01-12 N: month 1-12 D: day 01-31 J: day 1-31 **

H: 24 hours H: 12 hours I: minute 00-59 s: second 00-59 W: Day 0-6

A: AM or PM A: AM or PM.

Case study:

//date -- Formats a local time /date
echo date("Y years m months d", the time ());echo date("Y/m/d", the time ());echo date("Y-m-d H:i:s");// Do not write the second argument defaults to the current timestamp
Copy the code

Chapter 3: Changing the default TIME zone for PHP

There are two ways to change the PHP default time zone:

Ini configuration file: date.timezone = PRC

Date_default_timezone_set (): — Sets the default timezone for all date-time functions in a script.

Such as: date_default_timezone_set (” PRC “); // China time zone.

Date_default_timezone_get (): — Get the current timezone

Case study:

date_default_timezone_set('PRC');// Set the Chinese time zone

//date -- Formats a local time /date
echo date("Y years m months d", the time ());echo date("Y/m/d", the time ());echo date("Y-m-d H:i:s");// Do not write the second argument defaults to the current timestamp
Copy the code

Chapter 4 uses microseconds to calculate PHP script execution times

1 ms = 0.001 seconds (s)

1 microsecond (μs) = 0.001 ms

1 nanosecond (ns) = 0.001 microsecond (μs)

Microtime – Returns the current Unix timestamp and the number of microseconds

Format: Mixed microtime ([bool get_AS_float])

Microtime () The current Unix timestamp and the number of microseconds.

If called without optional arguments, this function returns a string where the latter is the number of seconds since the Unix era (0:00:00 January 1, 1970 GMT) and the former is the microsecond portion.

If the argument is given and its value is equivalent to TRUE, microtime() returns a floating-point number truncated to decimal point 4 with seconds.

$start = microtime(true);

for($i=1; $i<10000; $i++) { $tmp =13333/234;
}

$end = microtime(true);
echo $end - $start;
Copy the code