略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: MongoDB\BSON\UTCDateTime::toDateTime

2024-11-11

MongoDB\BSON\UTCDateTime::toDateTime

(mongodb >=1.0.0)

MongoDB\BSON\UTCDateTime::toDateTimeReturns the DateTime representation of this UTCDateTime

说明

final public MongoDB\BSON\UTCDateTime::toDateTime(): DateTime

参数

此函数没有参数。

返回值

Returns the DateTime representation of this UTCDateTime. The returned DateTime will use the UTC time zone.

错误/异常

范例

示例 #1 MongoDB\BSON\UTCDatetime::toDateTime() example

<?php

$utcdatetime 
= new MongoDB\BSON\UTCDateTime(1416445411987);
$datetime $utcdatetime->toDateTime();
var_dump($datetime->format('r'));
var_dump($datetime->format('U.u'));
var_dump($datetime->getTimezone());

?>

以上例程的输出类似于:

string(31) "Thu, 20 Nov 2014 01:03:31 +0000"
string(17) "1416445411.987000"
object(DateTimeZone)#3 (2) {
  ["timezone_type"]=>
  int(1)
  ["timezone"]=>
  string(6) "+00:00"
}
add a noteadd a note

User Contributed Notes 3 notes

up
3
pablo5_diaz at hotmail dot com
6 years ago
The php Datetime object returns the timestamp with 10 digits and this method needs miliseconds in the timestamp.

Make sure that the timestamp has 13 digits.
up
2
emm dot deoudes at gmail dot com
5 years ago
//convert retrieved time back to local time
<?php
/********************constructor**********************************/
$orig_date = new DateTime('2016-06-27 13:03:33');
$orig_date=$orig_date->getTimestamp();
$utcdatetime = new MongoDB\BSON\UTCDateTime($orig_date*1000);
/********************retrieve time in UTC**********************************/
$datetime = $utcdatetime->toDateTime();
$time=$datetime->format(DATE_RSS);
/********************Convert time local timezone*******************/
$dateInUTC=$time;
$time = strtotime($dateInUTC.' UTC');
$dateInLocal = date("Y-m-d H:i:s", $time);
echo
$dateInLocal;
?>
output: 2016-06-27 13:03:33
up
1
zeeshanyshaikh at gmail dot com
5 years ago
$tz = new DateTimeZone('Asia/Kolkata'); //Change your timezone
$date = date("Y-m-d h:i:sa"); //Current Date
$a = new MongoDB\BSON\UTCDateTime(strtotime($date)*1000);
             
      $datetime = $a->toDateTime();
      echo '<pre>';print_r($datetime);echo '</pre>';

      $datetime->setTimezone($tz); //Set timezone
      $time=$datetime->format(DATE_ATOM);  //(example: 2005-08-15T15:52:01+00:00)
         
    echo "<br>". $time . "<br>";

OUTPUT :
//Output of datetime object
DateTime Object
(
    [date] => 2016-08-09 14:57:06.000000
    [timezone_type] => 1
    [timezone] => +00:00
)
//Output of time
2016-08-09T20:27:06+05:30

官方地址:https://www.php.net/manual/en/mongodb-bson-utcdatetime.todatetime.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3