Introduction to date and time functions in PHP

Source: Internet
Author: User

Introduction to date and time correlation functions in PHP

One, Unix timestamp

In most of the current UNIX systems, the current date and time are saved by saving the Greenwich Mean time from 0 o'clock January 1, 1970 to the current time in seconds, with 32 as the whole column. January 1, 1970 0 o'clock also known as the Unix era. Unix timestamps can also be used under Windows systems, such as timestamps, but problems may occur if the time is processed before 1970 or after 2038.

PHP in the processing of some data, especially the time type of data in the database format, often need to first convert the time type of data into a UNIX timestamp and then processing, in addition, different database systems on the time type of data can not be compatible conversion, then need to convert time to UNIX timestamp, Then the time stamp is manipulated, which realizes the cross-platform of different database systems.

Second, time conversion to time stamp

You can use the Strtotime () function to convert the date and time expressed in a string to the form of a timestamp.

int Strtotime (string $time [, int $now])

$time is a string that contains the English date format, $time values are ignored if there is a number of milliseconds. The value is given at a time relative to the $now parameter, and the current time of the system is used by default if not given.

If the given year is in the form of a two-digit number, the year value 0~69 indicates that 2000~2069,70~100 represents 1970~2000.

Another date-acquired Unix timestamp function is the mktime () function, which has the following syntax:

int mktime ([int, $hour [, int $minute [, int $second [, $int $month [, int $day [, int $year]] []])

If all parameters are empty, the current time is assumed to be the default.

Iii. Date and time of acquisition

1.date () function

The function of the date () function is to convert the timestamp to a specific date and time string in the given format.

String Date (string $format [, int $timestamp])

Description: $format Specifies the format of the date and time after conversion, $timestamp is the timestamp of the conversion, and if omitted, uses the local current time, which is the value of the time () function. The time () function returns the timestamp of the current time.


2.getdate () function

You can also get date and time information using the GETDATE () function.

Array getdate ([int, $timestamp])

Description: The $timestamp is the timestamp to convert, and the current time is used if not given. The function returns an array containing date and time information based on $timestamp.


Iv. other date and time functions

1. Calculation of date and time

Since timestamps are 32-bit integer data, the difference between two time values can be calculated by adding and subtracting timestamps.

2. Check the date

The Checkdate () function can be used to check whether a date data is valid, with the following syntax:

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

Note: The value of $year is from 1 to 32767, $month value is from 1 to one, and the $day value is within the number of days of the given $month value, in which case the leap year is taken into account. When the given date is a valid date, the function returns True, otherwise false is returned.

3. Set the time zone

The system defaults to Greenwich Mean time, so the current time may differ from the local time, and PHP provides a function date_default_timezone_set () that can modify the time zone.

BOOL Date_default_timezone_set (string $timezone _identifier)

Parameter $timezone_identifier is the time zone to be specified, the value available in mainland China is Asia/chongqing,asia/shanghai,asia/urumqi. Beijing time can use PRC.

Alternatively, you can modify the default time zone by modifying the PHP configuration file: Open php.ini, locate the Date_timezone option, remove the semicolon ";" in front of the option, and set the value of the option to the time zone identifier of the default time zone you want to set. Restart Apache after saving, the system default time zone is set up.


This article is from the "Rangers" blog, please be sure to keep this source http://ccnupxz.blog.51cto.com/8803964/1839762

Introduction to date and time functions in PHP

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.