PHP11 date and time

learning points

  • UNIX timestamp
  • Convert dates in other formats to UNIX timestamp format
  • Date calculation based on UNIX timestamp
  • Get and format the output date
  • Modify the default time of PHP
  • Use of microseconds

  

Unix timestamp

Related concepts

Unix timestamp: The total number of seconds since January 01, 1970 00:00:00 GMT until now.

Most 32-bit operating systems use 32-bit binary numbers to represent time. Unix timestamps for such systems can be used up to 01/19/2038 03:14:07 GMT (binary: 01111111 11111111 11111111 11111111). One second later, the binary number will become 10000000 00000000 00000000 00000000, an overflow error occurs, causing the system to misinterpret the time as 20:45:52 on December 13, 1901. This is likely to cause software failures or even system crashes. Systems that use 64-bit binary numbers to represent time (up to 292,277,026,596 December 04, 15:30:08 GMT) will not encounter this type of overflow problem.

Timing bug for iOS devices with 64-bit processors.

 

PHP converts date and time to UNIX timestamp

  • mktime() function

    Role: Get the Unix timestamp of a date

    Syntax format:

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") ]]]]]] )

  

    Features: Excellent performance for date operation and verification, and can automatically correct out-of-bounds input.

    Sample code:

//get current timestamp

echo mktime();//mktime() is not recommended without parameters, time() is recommended

echo time();

 

//timestamp to date

echo date("Y-m-d",time());

 

//mktime automatically corrects out of bounds

echo date("Y-m-d",mktime(0,0,0,12,36,2015)).'<br>';

echo date("Y-m-d",mktime(0,0,0,14,30,2015)).'<br>';

echo date("Y-m-d",mktime(0,0,0,1,1,2015)).'<br>';

echo date("Ymd",mktime(0,0,0,1,1,69)).'<br>';//<=70 is automatically parsed into 1970

echo date("Y-m-d",mktime(0,0,0,1,1,1969)).'<br>';//1969

  

  • strtotime() function

    Syntax format:

int strtotime(string time,[int now])

    Sample code:

// Parse the English text into a timestamp: support the English country date description string

echo date ( "Y-m-d", strtotime ( "8 may 2016" ) );//2016-05-08

echo date ( "Ymd", strtotime ( "last monday" ) ); // last Monday

echo date ( "Y-m-d", strtotime ( "now" ) );//现在

echo date ( "Y-m-d", strtotime ( "+1 day" ) );//明天

  

date calculation

  • Hands-on practice: making a countdown
  • Hands-on exercise: Given a birthday date, calculate the age
  • Hands-on practice: ten years ago
  • Hands-on practice: three months ago

 

Get date and time in PHP

getdate() function: get date/time information

 

 

date() function: date and time formatted output

Timestamps should be used as the standard format when dates and times need to be stored or calculated.

Grammar rules:

String date ( string format [,int timestamp] )

 

 

 

Modify PHP default time zone

  • Modify php.ini

    Before modification: date.timezone = Europe/Paris

    After modification: date.timezone = Etc/GMT-8

  • Set the time zone function date_default_timezone_set()
date_default_timezone_set('PRC');

echo date("Y年m月d日  H:i:s");

  

Calculate PHP script execution time using microseconds

Syntax format:

mixed microtime ([ bool $get_as_float ] )

   

Code example:

echo microtime();//Output microseconds + timestamp

echo '<br>***********<br>';

echo microtime(true);//Output timestamp. Microseconds

   

Hands-on practice: calendar design

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326522613&siteId=291194637