PHP learning note 10 (date time function)

(1) View date function extension

  • phpinfo()
<?php
phpinfo();
  • After opening the above page, you can see the following to prove that the installation date has been extended
  • Browser input php.net to access PHP documents

(2) Set time zone

  • Mode 1: modify the configuration file php.ini
    • D:\ITSoft\wamp64\bin\php\php7.0.10\php.ini
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = PRC

; http://php.net/date.default-latitude
;date.default_latitude = 31.7667

; http://php.net/date.default-longitude
;date.default_longitude = 35.2333

; http://php.net/date.sunrise-zenith
;date.sunrise_zenith = 90.583333

; http://php.net/date.sunset-zenith
;date.sunset_zenith = 90.583333
  • Mode 2: date default timezone set
<?php

echo 'Current time zone'.date_default_timezone_get(); //UTC

var_dump(date_default_timezone_set('Asia/Shanghai'));//true

echo 'Current time zone'.date_default_timezone_get(); //Asia/Shanghai
  • Mode 3: init set
    • Sets the value of the specified configuration option. This option keeps the new value while the script is running and restores it at the end of the script
<?php

echo ini_get('date.timezone'); //UTC

ini_set('date.timezone','Asia/Shanghai');

echo ini_get('date.timezone'); //Asia/Shanghai

(3) Common time function

  • Common parameters
  • date use
<?php

ini_set('date.timezone','Asia/Shanghai');

echo date('Y year m month d day H:i:s');//Separator at will
echo date('H:i:s a');//am pm
echo date('w');//week

//Determine leap year
$runNian = date('L');
if($runNian){
    echo 'It's a leap year';
}else{
    echo 'Not leap year';
}
  • Use of time mktime
<?php

//Get current timestamp
echo time();//1513757887

//Time stamp converted to date
echo date('Y-m-d H:i:s',time());

//Time in a day
echo date('Y-m-d H:i:s',time()+24*60*60);

//Gets the timestamp of the specified date
echo mktime(0,0,0,2,1,2016);//(h i s N J Y) the timestamps of 2016-2-1 0:0:0 can be omitted successively
  • strtotime use
<?php

echo date('Y-m-d H:i:s',time());//2017-12-20 08:30:58
echo date('Y-m-d H:i:s',strtotime('+1 day'));//2017-12-21 08:30:58
echo date('Y-m-d H:i:s',strtotime('-5 days'));//2017-12-15 08:30:58

echo date('Y-m-d H:i:s',strtotime('+1 month'));//2017-01-20 08:30:58
echo date('Y-m-d H:i:s',strtotime('+5 months'));//2017-05-20 08:30:58
echo date('Y-m-d H:i:s',strtotime("last Monday"));//2017-12-18 08:30:58

echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
  • microtime()
<?php

echo microtime(); //0.85666200 1513758991 two part subtle value timestamp
echo microtime(true);// 1513759077.8005
  • Other functions
<?php

//Get current date
print_r(getdate());
// The output is as follows
//Array (
//    [seconds] => 53
//    [minutes] => 40
//    [hours] => 8
//    [mday] => 20
//    [wday] => 3
//    [mon] => 12
//    [year] => 2017
//    [yday] => 353
//    [weekday] => Wednesday
//    [month] =>
//    December [0] => 1513759253
//)

<?php
//Verify the validity of the date
var_dump(checkdate(1,32,2014));//false 

(4) Use of time control

  • test.php
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>H5 New form control</title>
</head>
<body>
    <h1>H5 New form date time control</h1>
    <form action='doAction.php' method='post'>
        <input type="date" name="datetime1" id="time1"/><br>
        <input type="submit" value="Submission"/>
    </form>
</body>
</html>
  • doAction.php
<?php

print_r($_POST);

Tags: PHP

Posted on Sat, 02 May 2020 03:07:07 -0700 by seangamer