zoukankan      html  css  js  c++  java
  • MongoDBBSONUTCDateTime::toDateTime

    示例# 1 MongoDB BSON UTCDatetime:toDateTime()例子

    <?php
    
    $utcdatetime = new MongoDBBSONUTCDateTime(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"
    }
    //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 MongoDBBSONUTCDateTime($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
    

      

    $tz = new DateTimeZone('Asia/Kolkata'); //Change your timezone
    $date = date("Y-m-d h:i:sa"); //Current Date
    $a = new MongoDBBSONUTCDateTime(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
    

      

      

  • 相关阅读:
    Java Applet实现五子棋游戏
    CrawlScript脚本语言实现网络爬虫
    Eclipse集成Git的实践
    以图表形式分析和统计数据
    爬虫抓取分页数据的简单实现
    爬虫的简单实现
    使用htmlparser爬虫技术爬取电影网页的全部下载链接
    使用webcollector爬虫技术获取网易云音乐全部歌曲
    百度地图API-覆盖物
    百度地图API-控件
  • 原文地址:https://www.cnblogs.com/sunlong88/p/9359689.html
Copyright © 2011-2022 走看看