Summary of php Common Date and Time Function Examples

  • 2021-12-12 08:18:37
  • OfStack

In this paper, an example is given to describe the date and time functions commonly used in php. Share it for your reference, as follows:

Time stamp

I won't go into details about the timestamp. It is in the manual, that is, it can accurately represent a time point. When I do projects, I often use timestamps to represent data, which is more convenient. If it is saved as date-time data, it may be easier to display it, but if it is a certain year or month of this date, it is more troublesome.


<?php
echo time();// Return 1 A timestamp in seconds   : 1467968841
echo microtime(true);// Return 1 A timestamp in milliseconds   : 1467968841.4155
echo microtime();// Returns a timestamp with milliseconds and seconds   : 0.41553000 1467968841 Which is preceded by the fraction of milliseconds followed by the number of seconds 
?>

Timestamp formatting

1. date($format,$timestamp) Formatting a timestamp under the currently used time zone

2. gmdate($format,$timestamp) Specify the formatted timestamp under the UTC time zone, no matter what time zone the user uses

Concept of time zone: Time zone is set according to the place of time, China belongs to the East 8, so it is 8 hours faster than UTC (Greenwich Mean Time)

Gets and sets the time zone


date_default_timezone_get()// Gets the currently used time zone, which should be by default UTC
date_default_timezone_set( ' Asia/Shanghai')// Set the current time zone to Shanghai, Asia 


<?php
echo date("Y-m-d  Week N H:i:s a",$time);//2016-07-08  Week 5 10:44:26 am( Under the default time zone )
echo date("Y-m-d  Week N H:i:s a",0);//1970-01-01  Week 4 00:00:00( Under the default time zone )
echo gmdate("Y-m-d  Week N H:i:s a",0);//1970-01-01  Week 4 00:00:00( All in UTC Under time zone )
?>

Parse time format to timestamp

1. mktime (): Parse the established time format into a timestamp


<?php
echo mktime(19,15,0,7,8,2016);// Get 2016 Year 8 Month 7 Day 19 Hour 15 Points 0 Time stamp of seconds 
?>

2. strtotime (): Parses the date and time description of any English text into a timestamp


<?php
  echo strtotime("2016-7-8 19:15:0");// Get 2016 Year 8 Month 7 Day 19 Hour 15 Points 0 Time stamp of seconds 
  echo strtotime("now");// Get the current timestamp 
  echo strtotime("+1 day");// The current time is pushed back 1 Time stamp of days 
  echo strtotime("-1 day");// The current time is pushed forward 1 Time stamp of days 
?>

3. checkdate (): Detects whether the input date exists


<?php
var_dump(checkdate(2,29,2015));//2015 Year 2 Month 29 Day does not exist, so it is false
var_dump(checkdate(2,29,2016));//2016 Year 2 Month 29 Day exists, so it is true
?>

Case: Calculate the date difference between two dates

1. Use the timestamp to calculate


<?php
$day1 = "2016-7-8";
$day2 = "2016-7-1";
echo (strtotime($day1) - strtotime($day2))/(24 * 60 * 60);
?>

2. Calculate with DateTime class


<?php
$datetime1 = new DateTime('2016-7-8');
$datetime2 = new DateTime('2016-7-1');
$interval = $datetime2->diff($datetime1);
echo $interval->format('%R%a days');
?>

PS: Here are some time and date related tools for your reference:

Online date/day calculator:
http://tools.ofstack.com/jisuanqi/date_jisuanqi

Online date calculator/difference days calculator:
http://tools.ofstack.com/jisuanqi/datecalc

Online Date-Day Difference Calculator:
http://tools.ofstack.com/jisuanqi/onlinedatejsq

Unix timestamp (timestamp) conversion tool:
http://tools.ofstack.com/code/unixtime

For more readers interested in PHP related content, please check the topics on this site: "Summary of php Date and Time Usage", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Grammar", "Summary of PHP Operation and Operator Usage", "Introduction to php Object-Oriented Programming", "Introduction to php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: