PHP time function

 The function of the PHP time function is to return the current time and the Unix timestamp (Unix timestamp), which is the time from 00:00:00 on January 1, 1970, Greenwich Mean Time, to the current time, with seconds as the default Unit, the function of PHP time function is similar to mktime , but it is simpler. It can be used directly without setting parameters. It can be used with the date function to calculate some time difference, such as the date of next week or the date of next month. date. The time function is a built-in function of PHP and can be used without additional installation.


PHP time function syntax
time(parameter);
The parameters of the PHP time function are optional items and do not have to be written. The default execution method of the time function will use the number of seconds between the current time and the Unix epoch (00:00:00 on January 1, 1970) To represent.

PHP time function example
<?php
  echo'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'; //Web page encoding

  echo'current Unix timestamp:'.time().'< br>'; //Current Unix timestamp
 
  $WeekSeconds=time()+(24*60*60*7); //24 hours     x 60 minutes x 60 seconds x 7 days
  echo'Today's date is:'.date('Ym -d').'<br>';
  echo'Next week's date is:'.date('Ym-d',$WeekSeconds).'<br>'; 
?>
Output result
current Unix timestamp: 1412221065
Today's date is: 2014-10-02
Next week's date is: 2014-10-09
The output result of the example is for simulation reference only, not the latest calculation result. The example begins echo of that period is HTML Meta function, used to mark the page encoding, Traditional Chinese because the paradigm used in some browsers will become garbled, so just to echo this page encoding declaration. Then the important point comes. First, use echo to output the time function without any operation to obtain the current Unix timestamp , which is the Unix timestamp. This part is only to test the function of the time function, and then we need to use the characteristics of the time function to calculate The date of the next week, the variable $WeekSeconds is used to calculate the current Unix timestamp, plus the time of the week, in seconds.

Then the next line first outputs today’s date. This part uses the date function. Then we put $WeekSeconds into the parameter position of the date function, which is equivalent to the number of seconds in the next week calculated with time, and let the date function determine The actual date, the same technique can also be used to calculate the date of the next month! What needs attention is the number of days in the month, try it out!

Post a Comment

0 Comments