Home > Backend Development > PHP Tutorial > PHP time processing function

PHP time processing function

WBOY
Release: 2016-07-25 08:42:59
Original
709 people have browsed it
  1. //mktime()
  2. Convert date and time to unix timestamp
  3. //time()
  4. Get the unix timestamp of the current time
  5. echo date("Y-m-d",mktime(0 ,0,0,12,31,2013))."
    ";
  6. //Example: Calculate the age of a user by calculating the difference between two unix timestamps
  7. $year = 1991;
  8. //Suppose the user's date of birth is 1991.07.16
  9. $month = 07;
  10. $day = 16;
  11. $brithday = mktime(0,0,0,$month,$day,$year);
  12. //Replace the user Convert the date of birth to a unix timestamp
  13. $nowdate = time();
  14. //Get the unix timestamp of the current time
  15. $ageunix = $nowdate - $brithday;
  16. //Get the difference in timestamps
  17. $age = floor ($ageunix / (60*60*24*365));
  18. //The difference in timestamps divided by the number of seconds per year is the user's actual age
  19. echo "The user's age is ".$age."< ;br> The sunrise time of a certain day
  20. //date_sunset()
  21. The sunset time of a certain day
  22. //date()
  23. Format a local time and date
  24. //microtime()
  25. Return the current UNIX timestamp and microseconds
  26. / /The following class calculates the execution time of the program by obtaining the execution time of the two functions
  27. class Timer{
  28. private $startTime;
  29. private $stopTime;
  30. function __construct(){
  31. $this->startTime = 0;
  32. $this->stopTime = 0;
  33. }
  34. function start(){
  35. $this->startTime = microtime(true);
  36. }
  37. function stop(){
  38. $this->stopTime = microtime(true) ;
  39. }
  40. function spent(){
  41. return round(($this->startTime - $this->stopTime),4);
  42. }
  43. }
  44. $timer = new Timer();
  45. $timer-> ;start();
  46. usleep(1000);
  47. $timer->stop();
  48. echo "Time to execute script".$timer->spent()."seconds" ;
  49. ?>
  50. Copy code
PHP

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template