方法:
<?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
添付ファイル:DateTimeクラスの使用に関する詳細な説明
DataTime
クラスdate()
、strtotime()
、gmdate()
などの機能と同じ機能を持って、処理日時が使用されますが、DateTime
クラスがより直感的で便利ですが、それは将来のPHP5.2.0の中に使用することが推奨されDateTime
たクラスの代わりに、対応する機能。
DateTime
クラスの使い方を見てみましょう。
1.現在のシステム時刻を取得し、印刷します
|
実行結果は次のとおりです。2014-08-2512:58:29
2.特定の時間を取得して印刷します
|
実行結果:2014-05-04 00:00:00 2014-08-26 00:00:00 2014-08-27 13:13:34
またはDateTimeの他のメソッドを介して
|
実行結果は次のとおりです。2014-08-2615:02:572014-08-27 15:02:57 1989-11-10 15:02:57 1989-11-10 11:10:10
3.UNIXタイムスタンプの変換
現在の時刻のタイムスタンプを取得します
|
実行結果は次のとおりです。1408950651
または
|
タイムスタンプを読み取り可能な時間に変換する
|
実行結果は次のとおりです。2014-08-2515:10:51
または
|
4.日付の比較
日付サイズの比較
|
日付間隔
|
执行结果为: 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