DateTime with microseconds

后端 未结 10 2111
长发绾君心
长发绾君心 2020-12-05 10:02

In my code, I\'m using DateTime objects to manipulate dates, then convert them to timestamp in order to save them in some JSON files.

For some reasons,

10条回答
  •  时光取名叫无心
    2020-12-05 10:33

    /*
     * More or less standard complete example. Tested.
     */
      private static function utime($format, $utime = null, $timezone = null) {
        if(!$utime) {
          // microtime(true) had too fiew digits of microsecconds
          $time_arr = explode( " ", microtime( false ) );
          $utime = $time_arr[1] . substr( $time_arr[0], 1, 7 );
        }
        if(!$timezone) {
          $timezone = date_default_timezone_get();
        }
        $date_time_zone = timezone_open( $timezone );
        //date_create_from_format( "U.u", $utime ) - had a bug with 3-rd parameter
        $date_time = date_create_from_format( "U.u", $utime );
        date_timezone_set( $date_time, $date_time_zone );
        $timestr = date_format( $date_time, $format );
        return $timestr;
      }
    

提交回复
热议问题