Blogger Information
Blog 29
fans 0
comment 0
visits 35025
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP实例化时间对象 DateTime
小臣
Original
748 people have browsed it

实例

<?php
/*
	PHP 时间
	实例化 DateTime
 */
// 输出当前时间
$datetime = new \DateTime;
print_r($datetime);
echo('当前时间:'.$datetime->format('Y-m-d H:i:s'));
echo '<br/>';
// 输出指定时间
$datetime = new \DateTime('2018-06-13');
echo '<pre/>';
print_r($datetime);
echo '<br/>';
// 根据给定的时间格式化为自己想要的时间格式
$datetime = \DateTime::createFromFormat('Ymd', '20180618');
print_r('自定义时间格式:'.$datetime->format('Y-m-d'));
echo '<br/>';
// 根据给定的时间戳格式化为给定的时间格式
$datetime = new \DateTime();
$datetime->setTimestamp(1465783744);
echo $datetime->format('Y-m-d H:i:s'),'<br/>';
// 两个日期时间比对,年与年比对,月与月比对……
$datetime1 = new \DateTime('2017-01-01 10:11:18');
$datetime2 = new \DateTime('2018-05-11 22:21:21');
$interval = $datetime1->diff($datetime2);
// print_r($interval->y);
echo '两个时间对比','<br/>';
print_r($interval->format('%Y')); // 年月日时分秒正常使用y,m,d,h,i,s
echo '<br>';
$datetime = new \DateTime();
$datetime->setDate(2018, 2, 28);
echo $datetime->format('Y-m-d'),'<br/>';
// 更改时间的时区
$timezone = new \DateTimeZone('Asia/Calcutta');
$datetime = new \DateTime();
$datetime->setTimezone($timezone);
print_r($datetime->format('Y-m-d H:i:s'));
echo '<br/>';
// 获取当前时区
$date = new \DateTime(null, new DateTimeZone('Asia/Shanghai'));
$tz = $date->getTimezone();
echo '当前时区:',$tz->getName(),'<br/>';
// 计算两个时区的偏移值
$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); // 秒数
echo '<br/>';

运行实例 »

点击 "运行实例" 按钮查看在线实例


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post