php date and time functions

System time zone settings for PHP date and time functions

In PHP, there are two ways to change the time zone:

The first:

modify php.ini For the settings in the file, click data; date.timezone =Option, remove the preceding quotation marks, and change to:“ date.timezone =Asia / Shanghai "and restart the Apache server


In the application, you need to add the following functions before using the time date function


echo 'utc Time:'.date('Y-m-d H:i:s').'<br>';
echo 'Beijing time:'.date('Y-m-d H:i:s');

The results of the operation are:

utc time: May 22, 2020 01:40:22
 Beijing time: May 22, 2020 09:40:22

If the program is uploaded to the space, it cannot be modified when setting the system time zone php.ini File, only date can be used_ default_ timezone_ The set() function sets the time zone.


PHP time() function get current timestamp instance details

Note: the typical range of valid timestamps is 20:45:54, December 13, 1901-03:14:07, January 19, 2038 (this range conforms to the minimum and maximum values of 32-bit signed integers). In Windows system, this range is limited to January 1, 1970-january 19, 2038.

Usage of time() function

In PHP, get the current UNIX timestamp through the time() function. The return value is the number of seconds from the timestamp era (00:00:00, January 1, 1970, GMT) to the current time.

Get current timestamp instance

In this example, we use the time() function to get the current local timestamp and format the timestamp output. The code is as follows


echo 'Time stamp:'.time().'<br>';
echo 'Format time:'.date('Y-m-d H:i:s',time()).'<br>';
$nexttime = time()+(7*24*60*60);
echo 'One week later:'.date('Y-m-d H:i:s',$nexttime);

The code runs as follows:

Time stamp: 1590114337
 Format time: May 22, 2020 02:25:37
 One week later: 2020-05-29 02:25:37


PHP date() function get current date and time instance details

date function instance 1

This example shows you the usage of the date() function. The code is as follows


echo 'now: '.date('Y-m-d').'<br>';

Code run result:

now: 2020-05-22


date function instance 2

This example will compare the output of each constant. The example code is as follows


echo 'DATE_ATOM: '.date(DATE_ATOM).'<br>';
echo 'DATE_COOKIE: '.date(DATE_ATOM).'<br>';
echo 'DATE_ISO8601: '.date(DATE_ATOM).'<br>';
echo 'DATE_PRC822: '.date(DATE_ATOM).'<br>';
echo 'DATE_PRC850: '.date(DATE_ATOM).'<br>';
echo 'DATE_RSS: '.date(DATE_ATOM).'<br>';
echo 'DATE_W3C: '.date(DATE_ATOM).'<br>';

Code run result:

DATE_ATOM: 2020-05-22T02:29:49+00:00
DATE_COOKIE: 2020-05-22T02:29:49+00:00
DATE_ISO8601: 2020-05-22T02:29:49+00:00
DATE_PRC822: 2020-05-22T02:29:49+00:00
DATE_PRC850: 2020-05-22T02:29:49+00:00
DATE_RSS: 2020-05-22T02:29:49+00:00
DATE_W3C: 2020-05-22T02:29:49+00:00


php getdate() function get date information instance details

Usage of getdate() function:

The getdate() function gets information about the specified part of the date.

Description of the associated array element returned by getdate() function



getdate() function instance

Example 1

In this example, getdate() function is used without parameters. The code is as follows:



The operation results are as follows:



Example 2

Use the getdate() function to get the current date information of the system and output the return value of the function. The example code is as follows:


$arr = getdate();
echo $arr['year'].'/'.$arr['mon'].'/'.$arr['mday'].' ';
echo $arr['hours'].':'.$arr['minutes'].':'.$arr['seconds'].' '.$arr['weekday'].'<br>';
echo 'today is the '.$arr['yday'].' day of year';

The operation results are as follows:

2020/5/22 2:34:15 Friday
today is the 142 day of year


The php checkdate() function verifies the validity of the date

checkdate() function syntax:

The checkdate() function checks the validity of the date.


$year = 2020;
$month = 4;
$day = 30;

$year = 2020;
$month = 4;
$day = 31;

The operation results are as follows:

D:\phpstudy_pro\WWW\test\test.php:7:boolean true
D:\phpstudy_pro\WWW\test\test.php:12:boolean false

Pay attention to the order of parameters


You can also write the date directly in the checkdate() function




php strtotime() function parsing date and time to UNIX timestamp instance details

strotime() function instance

Example 1

Use the strotime() function to get the unix timestamp of the specified date. The code is as follows


echo strtotime('2020-5-20');

Operation result:



Example 2

In this example, the strotime() function is used to obtain the UNIX time stamp of the date time string in English format and output part of the time. The example code is as follows


echo strtotime('2020-5-22');echo '<br>';
echo strtotime('now');echo '<br>';
echo strtotime('22 May 2020');echo '<br>';
echo strtotime('+7 day');echo '<br>';
echo strtotime('+1 week');echo '<br>';
echo strtotime('+1 week 2 days 3hours 4seconds');echo '<br>';
echo strtotime('next Friday');echo '<br>';
echo strtotime('last Friday');

Code run result:


Tags: PHP Unix Apache Windows

Posted on Thu, 21 May 2020 22:26:51 -0700 by rklapwijk