method:
<?php
/*
* 时区转换
*/
function toTimeZone($src = '2021-01-14 00:00:00', $from_tz = 'America/Denver', $to_tz = 'Asia/Shanghai', $fm = 'Y-m-d H:i:s') {
$datetime = new DateTime($src, new DateTimeZone($from_tz));
$datetime->setTimezone(new DateTimeZone($to_tz));
return $datetime->format($fm);
}
$dateTime = toTimeZone('2021-01-14 00:00:00');
die($dateTime);
# 输出: 2021-01-14 15:00:00
Attachment: Detailed explanation of the use of DateTime class
DataTime
Class with date()
, strtotime()
, gmdate()
etc. have the same function as the function, the processing date and time are used, but the DateTime
class is more intuitive and convenient, it is recommended to use in the future PHP5.2.0 DateTime
class instead of the corresponding function.
Let's take a look at DateTime
the usage of the class.
1. Get the current system time and print it
|
The execution result is: 2014-08-25 12:58:29
2. Get a specific time and print
|
The execution result is: 2014-05-04 00:00:00 2014-08-26 00:00:00 2014-08-27 13:13:34
Or through other methods of DateTime
|
The execution results were: 2014-08-26 15:02:57 2014-08-27 15:02:57 1989-11-10 15:02:57 1989-11-10 11:10:10
3. Conversion of unix timestamp
Get the timestamp of the current time
|
The execution result is: 1408950651
or
|
Convert timestamp to readable time
|
The execution result is: 2014-08-25 15:10:51
or
|
4. Date comparison
Date size comparison
|
Date interval
|
执行结果为: DateInterval Object ( [y] => 0 [m] => 0 [d] => 20 [h] => 7 [i] => 35 [s] => 50 [weekday] => 0 [weekday_behavior] => 0 [first_last_day_of] => 0 [invert] => 0 [days] => 20 [special_type] => 0 [special_amount] => 0 [have_weekday_relative] => 0 [have_special_relative] => 0 )
将返回的对象格式化输出
|
输出结果为:The future will come in 00 years 0 months and 20 days