In this paper, the methods used by the DateTime object are collated to make it easy for us to find and flip through, so we can refer to learning.
Objective
The instantiation object is preceded by \ means that the native class is used in the namespace, and if no namespace is used, the previous \ is deleted
1. Output Current time
$datetime = new \datetime;print_r ($datetime->format (' y-m-d h:i:s '));
2. Output a given time
$datetime = new \datetime (' 2016-06-13 ');p rint_r ($datetime);
3. Make the time you want according to the given time format
$datetime = \datetime::createfromformat (' Ymd ', ' 20160618 ');p rint_r ($datetime->format (' y-m-d '));
4. Output the UNIX timestamp format (Method 1 Returns a negative number if it was before 1990, and Method 2 returns false)
Method 1 (php5.2): $datetime = new \datetime (), Echo $datetime->format (' U '), exit;//Method 2 (php5.3) Recommended $datetime = new \ DateTime (); Echo $datetime->gettimestamp (); exit;
5. Format a given time based on a given timestamp
$datetime = new \datetime (); $datetime->settimestamp (1465783744); Echo $datetime->format (' y-m-d h:i:s ');
6. Two date and time ratio, year to year, month to month.
$datetime 1 = new \datetime (' 2016-01-01 10:11:18 '); $datetime 2 = new \datetime (' 2017-05-11 22:21:21 '); $interval = $ Datetime1->diff ($datetime 2);p Rint_r ($interval->format ('%Y ')),//% means using formatting, r indicates whether the date is greater than (+), or less than (-). A is greater than or less than how many days, seconds and minutes normal use y,m,d,h,i,s
7. Create a time that is longer than a few days ago
The argument to the DateInterval constructor is a string that represents the time interval convention, which begins with the letter p, followed by an integer, and finally a period identifier, qualifying the preceding integer. Valid period identifiers are as follows: Y (year) m (months) D (days) W (weeks) H (time) m (min) S (s) interval conventions can have both time and date, if there is time need to add the letter T between date and time, for example, the interval convention p2d represents the interval of two days, The interval convention p2dt5h2m represents an interval of two days, five hours, and two minutes.
$datetime = new \datetime (), $interval = new \dateinterval (' p2dt5h ');//or using createfromdatestring method//$interval = \ Dateinterval::createfromdatestring (' 1 month ');//Modify a DateTime instance $datetime->add ($interval); Echo $datetime Format (' y-m-d h:i:s ');
8. Create a few days ago
$datetime = new \datetime (), $interval = new \dateinterval (' p2dt5h '); $datetime->sub ($interval); Echo $datetime Format (' y-m-d h:i:s ');//ps: There is a modify method, this method is minus 30, not like a forward 1 days, output or December $datetime = new \datetime (' 2014/12/31 '); Datetime->modify ('-1 month ');p Rint_r ($datetime); exit;
9. Resets the current DateTime object's time to a different date, passing the year, month, day
$datetime = new \datetime (); $datetime->setdate (2); Echo $datetime->format (' y-m-d '); exit;
10. Resets the current DateTime object's time at different times, passing, minutes, seconds (optional parameters)
$datetime = new \datetime (), $datetime->settime (), Echo $datetime->format (' y-m-d h:i:s '); exit;
11. Time zone before formatting time to change
$timezone = new \datetimezone (' Asia/calcutta '); $datetime = new \datetime (); $datetime->settimezone ($timezone); Print_r ($datetime->format (' y-m-d h:i:s '); exit;
12. Return to time zone
$date = new \datetime (null, New Datetimezone (' Asia/shanghai ')), $tz = $date->gettimezone (); Echo $tz->getname ();
13. Calculate offset values for two time zones
$dateTimeZoneTaipei = new \datetimezone ("Asia/taipei"); $dateTimeZoneJapan = new \datetimezone ("Asia/tokyo"); $ Datetimetaipei = new \datetime ("Now", $dateTimeZoneTaipei); $dateTimeJapan = new \datetime ("Now", $dateTimeZoneJapan); $ Timeoffset = $dateTimeZoneJapan->getoffset ($dateTimeTaipei);p rint_r ($timeOffset); exit;
14. Return time interval, how long
$interval = new \dateinterval (' p2y4dt6h8m '); Echo $interval->format ('%d days ');
15. The iteration outputs the date of the previous days from the current date.
The Dateperiod class constructs a method that accepts three parameters and must provide a DateTime instance that represents the date and time that the iteration started, an integer that represents the interval of the next date and time, and the total number of iterations of the fourth parameter is optional, Used to explicitly specify the end date and time of the cycle, and if you want to exclude the start date and time from the iteration, you can set the last parameter of the constructor to a DatePeriod::EXCLUDE_START_DATE
constant:
$datetime = new \datetime (); $interval = \dateinterval::createfromdatestring ('-1 day '); $period = new \dateperiod ($ DateTime, $interval, 3), foreach ($period as $date) { echo $date->format (' y-m-d '), Php_eol;}