Coder Social home page Coder Social logo

microtime's Introduction

Microtime

Microtime is a library written in pure PHP that provides an interface and a class to work with microseconds. PHP offers the possibility to get the current timestamp in seconds and microseconds via the microtime() function, but the resulting two values must always be converted so that the number of microseconds since the unix epoch is obtained. The Microtime class does this work for you. This allows you to create a Microtime object from different formats and convert it into different formats like DateTime object, integer and string.

Requirements

Microtime requires the following:

  • PHP 7.1 or higher

Installation

Microtime is installed via Composer. To add a dependency to Microtime in your project, either

Run the following to use the latest stable version

    composer require reply/microtime

or if you want the latest master version

    composer require reply/microtime:dev-master

You can of course also manually edit your composer.json file

{
    "require": {
       "reply/microtime": "^2.0"
    }
}

Getting started

The following is a basic usage example of the Microtime library:

<?php

use Reply\Microtime\Microtime;

// Creates a Microtime object from current timestamp
$microtime = Microtime::fromNow();

// Creates a Microtime object from a string containing seconds and microseconds
$microtime = Microtime::fromString('0.34497500 1589445224');
// or
$microtime = Microtime::fromMicrotime('0.34497500 1589445224');

// Creates a new Microtime object from an integer containing the microseconds since the unix epoch
$microtime = Microtime::fromInt(1589445224344975);
// or
$microtime = Microtime::fromMicroseconds(1589445224344975);

// Creates a new Microtime object from a float containing the result of the microtime(true) function
$microtime = Microtime::fromFloat(1589445622.7431);

// Creates a new Microtime object from an integer containing the seconds since the unix epoch
$microtime = Microtime::fromSeconds(1589445622);

// Creates a new Microtime object from a DateTime object
$microtime = Microtime::fromDateTime(new \DateTime());

// Returns the microtime as string
$string = $microtime->toString();
// or
$string = (string) $microtime;
// or
$string = "Time in microseconds: $microtime";

// Returns the microtime as integer
$int = $microtime->toInt();

// Returns the microtime as float
$float = $microtime->toFloat();

// Returns the microtime as string in the format of the microtime() function
$string = $microtime->toMicrotime();

// Returns the microtime in a DateTime object
$dateTime = $microtime->toDateTime();

// Returns the microtime in a DateTimeImmutable object
$dateTimeImmutable = $microtime->toDateTimeImmutable();

Contributing

You're welcome to contribute to Microtime. Below are some of the things that you can do to contribute.

microtime's People

Contributors

floplus avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

floplus

microtime's Issues

Optimize fromMicrotime

The fromMicrotime method calls the constructor with a formatted date string. That means, that two DateTime objects need to be created. In the feature, the constructor could additionally allow DateTimeInterface as parameter to build the object.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.