AthenaeumAthenaeum
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
  • Version 6.x

    • Release Notes
    • Upgrade Guide
    • New to this...
    • Contribution Guide
    • Security Policy
    • Code of Conduct
    • Origin
  • ACL

    • Introduction
    • How to install
    • Setup
    • Permissions
    • Roles
    • Users
    • Cached Permissions
  • Audit

    • Audit
    • How to install
    • Setup
    • Recording
    • Events
  • Circuits

    • Circuits
    • How to install
    • Setup
    • Usage
    • Events
  • Collections

    • Collections
    • How to install
    • Summation

      • Summation Collection
      • Items Processor
  • Config

    • Configuration Loader
    • How to install
    • Setup
    • Load Configuration Files
    • Custom File Parsers
  • Console

    • Command and Schedule Registration
    • How to install
    • Setup
    • Commands
    • Schedules
  • Container

    • IoC Service Container
    • How to install
    • Container
    • List Resolver
  • Core

    • Athenaeum Core Application
    • How to install
    • Setup
    • Usage

      • Configuration
      • Service Providers
      • Service Container
      • Events
      • Caching
      • Logging
      • Console
      • Task Scheduling
      • Exception Handling
      • Extending Core Application
      • Testing
  • Database

    • Introduction
    • How to install
    • Models

      • Instantiatable
      • Sluggable
    • Query

      • Criteria (Query Filter)
  • Dto

    • Data Transfer Object (DTO)
    • How to install
    • Create Interface
    • Implement DTO
    • How to use
    • Populate
    • Export
    • Json
    • Serialization
    • Nested DTOs
    • Array DTO
  • ETags

    • ETags
    • How to install
    • Setup
    • Usage
    • Generators

      • Default Generator
      • Custom Generator
    • Eloquent Models
    • Macros
  • Events

    • Register Listeners and Subscribers
    • How to install
    • Setup
    • Listeners
    • Subscribers
  • Filters

    • Search Filter Utilities
    • Prerequisites
    • How to install
    • Setup
    • Processor
    • Filters Builder
    • Predefined Resources

      • Search Processor
      • Sorting Processor
      • Constraints Processor
      • Matching Processor
    • Tip: Create a base builder
  • Flysystem

    • Introduction
    • Database Adapter

      • Introduction
      • How to install
      • Setup
      • Data Deduplication
      • MIME-Type Detection
  • Http

    • Api

      • Http API
      • How to install
      • Setup
      • Resources

        • Introduction
        • Timestamps
        • Self-Link
        • Relations
        • Registrar
      • Middleware

        • Introduction
        • Request Must Be Json
        • Capture Fields To Select
    • Clients

      • Http Clients
      • How to install
      • Setup
      • Basic Usage
      • Available Methods

        • Fluent Api
        • Protocol Version
        • Base Uri
        • Http Method and Uri
        • Headers
        • Accept & Content-Type
        • Authentication
        • Http Query
        • Payload Format
        • Payload
        • Attachments
        • Cookies
        • Response Expectations
        • Middleware
        • Conditions
        • Criteria
        • Redirects
        • Timeout
        • Debugging
        • Logging
        • Driver Options
        • Driver
      • Http Query Builder

        • Introduction
        • Select
        • Where
        • Dates
        • Include
        • Pagination
        • Sorting
        • Raw Expressions
        • Custom Grammar
    • Cookies

      • Http Cookies
      • How to install
      • Usage
    • Messages

      • Http Messages
      • How to install
      • Serializers
  • Maintenance

    • Modes

      • Maintenance Modes
      • How to install
      • Setup
      • Basic Usage
      • Available Drivers
  • Mime Types

    • MIME-Types
    • How to install
    • Setup
    • Usage
    • Drivers

      • Available Drivers
      • File Info
  • Properties

    • Properties Overload
    • How to install
    • Usage
    • Naming Convention
    • Properties Visibility
  • Redmine

    • Redmine Api Client
    • How to install
    • Setup
    • General Usage

      • Supported Operations
      • Fetch list of resources
      • Find
      • Fetch
      • Create new record
      • Update existing record
      • Delete existing record
      • Relations
    • Available Resources

      • Predefined Resources
      • Attachments
      • Enumerations
      • Issue Relations
      • Users
      • User Groups
      • Roles
      • Project Memberships
      • Versions (Milestones)
      • Issue Categories
      • Trackers
  • Service

    • Service Registrar
    • How to install
    • How to use
  • Streams

    • Streams
    • How to install
    • Setup
    • How to use

      • Introduction
      • Open and Close
      • Raw Resource
      • Seeking
      • Reading
      • Writing
      • Size
      • Truncate
      • Flush
      • Hash
      • MIME-Type
      • Output
      • Locking
      • Transactions
      • Meta
      • Misc
  • Support

    • Introduction
    • How to install
    • Laravel Aware-of Helpers

      • How to use
      • Enforce Via Interface
      • Custom Default
      • Pros and Cons
      • Available Helpers
    • Aware-of Properties

      • Generator
      • Available Aware-of Helpers
    • Live Templates
  • Testing

    • Introduction
    • How to install
    • Test Cases
    • Testing Aware-of Helpers
  • Utils

    • Introduction
    • How to install
    • Array
    • Duration
    • Json
    • Math
    • Memory
    • Method Helper
    • Invoker
    • Populatable
    • String
    • Version
  • Validation

    • Introduction
    • How to install
    • Setup
    • Rules

      • Alpha-Dash-Dot
      • Semantic Version
You are viewing documentation for an outdated version. It is no longer supported!

Duration

Duration is a helper for dealing with relative time.

  • Example
  • Creating new instance
    • from()
    • fromString()
    • fromSeconds()
    • fromMinutes()
    • fromHoursMinutes()
    • fromDifference()
  • Convert
  • Format
  • Onward

Example

use Aedart\Utils\Dates\Duration;

$a = new DateTime("2020-09-23 + 42 seconds + 23456 microseconds");
$b = new DateTime("2020-09-23 - 5 hours - 6 minutes");

$duration = Duration::fromDifference($a, $b);

echo $duration->format('%r%Y-%M-%D %H:%I:%S.%F'); //  '-00-00-00 05:06:42.023456'

Creating new instance

You can create a new Duration instance, using a variety of methods. The following demonstrates some of these:

$duration = Duration(42); // Using seconds
$duration = Duration(new DateInterval('P10Y7DT4H5M34S')); // Using DateInterval
$duration = Duration(new DateTime('@' . (42 * 60))); // Using DateTime

from()

The from() is a static alias for the Duration classes constructor.

$duration = Duration::from(new DateInterval('P10Y7DT4H5M34S'));

fromString()

fromString() can be used to create a new Duration instance, using a data and time string.

$duration = Duration::fromString('@' . (42 * 60));

fromSeconds()

As the name implies, fromSeconds() returns a new instance from specified amount of seconds.

$duration = Duration::fromSeconds(3600);

fromMinutes()

To create a new Instance from minutes, use fromMinutes().

$duration = Duration::fromMinutes(5);

fromHoursMinutes()

To create an instance from hours and optional minutes, use the fromHoursMinutes() method.

$duration = Duration::fromHoursMinutes(2, 30); // Minutes are optional

Should you have a hours and minutes string, like 02:30 or 1:25, then use the fromStringHoursMinutes() method to create duration instance.

$duration = Duration::fromStringHoursMinutes('02:25');

fromDifference()

When you wish to calculate the difference between two dates (or times), use the fromDifference().

$then = new DateTime("2020-09-23 - 5 hours - 6 minutes");
$when = new DateTime("2020-09-23 + 42 seconds + 23456 microseconds");

$duration = Duration::fromDifference($when, $then);

Convert

To obtain the duration in minutes, seconds or other format, use can use the following:

$duration = Duration::from(52200);

echo $duration->asSeconds(); // 52200
echo $duration->asMinutes(); // 870
echo $duration->toHoursMinutes(); // '14:30'
echo $duration->toHoursMinutes(true); // '14 hours 30 minutes'
echo $duration->toDaysHoursMinutes(); // '0-14:30'
echo $duration->toDaysHoursMinutes(true); // '0 days 14 hours 30 minutes'

Format

The format() can be used to format the duration into a string. Behind the scene, PHP native DateInterval::format is used.

$duration = Duration::from(new DateInterval('P10Y7DT4H5M34S'));

echo $duration->format('%Y-%M-%D %H:%I:%S'); // '10-00-07 04:05:34'

Onward

Please review the source code for additional methods and examples.

Edit page
Last Updated: 16/02/2023, 09:10
Contributors: Alin Eugen Deac, alin
Prev
Array
Next
Json