Use of the datetime class in Yii2, yii2datetime class
Preface
In the past, the date method was often used to change the time format. Later I learned that the datetime class in php was used to process the time format and record the usage for future use.
Instantiation
In yii 1.1, you can directly write
$datetime = new DateTime;
But in yii2, an error was reported directly, saying that this class could not be found. Later I checked that it had something to do with the namespace. So in the namespace framework, I should write it like this and add a slash.
$datetime = new \DateTime;
Use
The following lists the formats that can be output by this class.
Output Current Time
$datetime = new \DateTime; print_r($datetime->format('Y-m-d H:i:s'));
Output the given time
$datetime = new \DateTime('2016-06-13');print_r($datetime);
Format the desired time according to the given time
$datetime = \DateTime::createFromFormat('Ymd', '20160618');print_r($datetime->format('Y-m-d'));
Output Unix timestamp format (method 1 returns a negative number if it is before 1990, and method 2 returns false)
// Method 1 (php5.2): $ datetime = new \ DateTime (); echo $ datetime-> format ('U'); exit; // method 2 (php5.3) $ datetime = new \ DateTime (); echo $ datetime-> getTimestamp (); exit;
Format the time according to the given Timestamp
$datetime = new \DateTime();$datetime->setTimestamp(1465783744);echo $datetime->format('Y-m-d H:i:s');
Comparison of the two dates and times, year-to-year comparison, month-to-month comparison ......
$ Datetime1 = new \ DateTime ('2017-01-01 10:11:18 '); $ datetime2 = new \ DateTime ('2017-05-11 22:21:21 '); $ interval = $ datetime1-> diff ($ datetime2); print_r ($ interval-> format ('% y'); // % indicates formatting, R indicates whether the value is greater than this date (+) or less than this date (-). // a indicates the number of days greater than or less than this value. y, m, d, h, i, s
Created several days ago
The parameter of the DateInterval constructor is a string that represents the time interval Convention. This time interval convention starts with the letter P, followed by an integer, and finally a periodic identifier, which limits the previous integer. Valid Period identifier: Y (year) M (month) D (day) W (week) H (hour) M (minute) S (second) there can be both time and date in the interval Convention. If there is time, you need to add a letter T between the date and time. For example, the interval Convention P2D indicates that the interval is two days, p2DT5H2M indicates that the interval is two days, five hours, and two minutes.
$ Datetime = new \ DateTime (); $ interval = new \ DateInterval ('p2dt5h '); // or use the createFromDateString method // $ interval = \ DateInterval :: createFromDateString ('1 month'); // modify the DateTime instance $ datetime-> add ($ interval); echo $ datetime-> format ('Y-m-d H: I: s ');
Created several 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, which is minus 30, it is not like pushing for 1 day before, but the output is December $ datetime = new \ DateTime ('2014/1/31'); $ datetime-> modify ('-1 month '); print_r ($ datetime); exit;
Resets the time of the current DateTime object to different dates, passing the year, month, and day
$datetime = new \DateTime();$datetime->setDate(2015, 2, 28);echo $datetime->format('Y-m-d');exit;
Resets the time of the current DateTime object at different times, passing time, minute, second (optional parameter)
$datetime = new \DateTime();$datetime->setTime(20, 20, 24);echo $datetime->format('Y-m-d H:i:s');exit;
Time zone of time changed before formatting time
$timezone = new \DateTimeZone('Asia/Calcutta');$datetime = new \DateTime();$datetime->setTimezone($timezone);print_r($datetime->format('Y-m-d H:i:s'));exit;
Return Time Zone
$date = new \DateTime(null, new DateTimeZone('Asia/Shanghai')); $tz = $date->getTimezone(); echo $tz->getName();
Calculate the offset of 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);print_r($timeOffset);exit;
Returned Time Interval
$interval = new \DateInterval('P2Y4DT6H8M');echo $interval->format('%d days');
Iteration outputs the date a few days before the current date.
The construction method of the DatePeriod class accepts three parameters and must provide a DateTime instance, indicating that the start date and time of the iteration are a DateInterval instance, indicating that the interval between the next date and time is an integer, indicates the total number of iterations. The fourth parameter is optional and is used to explicitly specify the end date and time of the cycle. If you want to exclude the start date and time during iteration, you can set the last parameter of the constructor to the constant DatePeriod: EXCLUDE_START_DATE:
$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;}