PHP date and time are converted according to the specified time zone and the use of DateTime class is explained in detail

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

DataTimeClass with date(), strtotime(), gmdate()etc. have the same function as the function, the processing date and time are used, but the DateTimeclass is more intuitive and convenient, it is recommended to use in the future PHP5.2.0 DateTimeclass instead of the corresponding function.

Let's take a look at DateTimethe usage of the class.

1. Get the current system time and print it

 

<?php
$date = new DateTime();
echo $date->format('Y-m-d H:i:s');
?>

 

The execution result is: 2014-08-25 12:58:29

2. Get a specific time and print

 

<?php
$date = new DateTime('2014-05-04');
echo $date->format('Y-m-d H:i:s');
echo "\n";
$date2 = new DateTime('tomorrow');
echo $date2->format('Y-m-d H:i:s');
echo "\n";
$date2 = new DateTime('+2 days');
echo $date2->format('Y-m-d H:i:s');
?>

 

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

 

<?php
$date = new DateTime();
// add方法
$date->add(new DateInterval('P1D'));
echo $date->format('Y-m-d H:i:s');
echo "\n";
// modify方法
$date->modify('+1 day');
echo $date->format('Y-m-d H:i:s');
echo "\n";
// setDate方法
$date->setDate('1989-11-10');
echo $date->format('Y-m-d H:i:s');
echo "\n";
// setTime方法
$date->setTime('11:10:10');
echo $date->format('Y-m-d H:i:s');
echo "\n";
?>

 

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

 

<?php
$date = new DateTime();
echo $date->format('U');
echo "\n";
?>

 

The execution result is: 1408950651

or

 

<?php
$date = new DateTime();
echo $date->getTimestamp();
echo "\n";
?>

 

Convert timestamp to readable time

 

<?php
$date = new DateTime('@1408950651');
$date->setTimezone(new DateTimeZone('Asia/Shanghai'));
echo $date->format('Y-m-d H:i:s');
echo "\n";
?>

 

The execution result is: 2014-08-25 15:10:51

or

 

<?php
$date = new DateTime();
$date->setTimestamp(1408950651);
echo $date->format('Y-m-d H:i:s');
echo "\n";
?>

 

4. Date comparison

Date size comparison

 

<?php
$date1 = new DateTime();
$date2 = new DateTime('2014-09-15');

if($date1 < $date2) {
    echo $date2->format('Y-m-d H:i:s') . ' is in the future';
}
?>

 

Date interval

 

<?php
$date1 = new DateTime();
$date2 = new DateTime('2014-09-15');

$diff = $date1->diff($date2);
print_r($diff);
?>

 

执行结果为: 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 )

将返回的对象格式化输出

 

<?php
$date1 = new DateTime();
$date2 = new DateTime('2014-09-15');

$diff = $date1->diff($date2);
echo $diff->format("The future will come in %Y years %m months and %d days");
?>

 

输出结果为:The future will come in 00 years 0 months and 20 days

Guess you like

Origin blog.csdn.net/JineD/article/details/112615360