Git Product home page Git Product logo

daterange's Introduction

Gamajo Date Range

Latest Stable Version Total Downloads Latest Unstable Version License

Display a range of dates, with consolidated time parts.

Table Of Contents

Installation

The best way to use this package is through Composer:

composer require gamajo/daterange

Basic Usage

Create an instance of the DateRange class, with DateTimeImmutable or DateTime start and end date-time objects as arguments. Then choose the format to use as the end date output. The start date will only display the time parts that are not duplicated.

$dateRange = new DateRange(
    new DateTimeImmutable('23rd June 18 14:00'),
    new DateTimeImmutable('2018-06-23T15:00')
);
echo $dateRange->format('H:i d M Y'); // 14:00 – 15:00 23 Jun 2018

If the formatted date would be the same start and end date, only a single date is displayed:

$dateRange = new DateRange(
    new DateTimeImmutable('23rd June 18 14:00'),
    new DateTimeImmutable('2018-06-23T15:00')
);
echo $dateRange->format('jS M Y'); // 23rd Jun 2018

Advanced Usage

Change Separator

The default separator between the start and end date, is a space, en-dash, space: ' – '

This can be changed via the changeSeparator() method:

$dateRange = new DateRange(
    new DateTimeImmutable('23rd June 18 14:00'),
    new DateTimeImmutable('2018-06-23T15:00')
);
$dateRange->changeSeparator(' to ');
echo 'From ', $dateRange->format('H:i d M Y'); // From 14:00 to 15:00 23 Jun 2018

Change Removable Delimiters

The consolidation and removal of some time parts may leave delimiters from the format:

$dateRange = new DateRange(
    new DateTimeImmutable('23rd June 18'),
    new DateTimeImmutable('2018-06-24')
);
echo $dateRange->format('d·M·Y'); //  23·· – 24·Jun·2018

Be default, /, - and . are trimmed from the start date, but this can be amended with the changeRemovableDelimiters() method:

$dateRange = new DateRange(
    new DateTimeImmutable('23rd June 18'),
    new DateTimeImmutable('2018-06-24')
);
$dateRange->changeRemovableDelimiters('·');
echo $dateRange->format('d·M·Y'); //  23 – 24·Jun·2018

Known Issues

These are known issues which need addressing before this package can be considered stable:

Contributing

All feedback, bug reports and pull requests are welcome.

License

Copyright (c) 2018 Gary Jones, Gamajo

This code is licensed under the MIT License.

daterange's People

Contributors

garyjones avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

areeb111

daterange's Issues

Escaped characters are not handled correctly

A string like 'd~M\Y', 'd~M\\Y' and 'd~M\\\Y' are not handled correctly. Current unit tests fail, since the output includes Y, when it shouldn't.

A string like 'M j<\s\up>S</\s\up> Y' does pass, thanks to the crude attempt at handling escaped characters.

DateFormat::isEscapedCharacter() function needs revisiting.

'M jS Y' should give Jun 25th – 28th 2018

@AMNDesign wants to have a format of M jS Y (albeit with <sup></sup> tags, but that's redundant here) and have an output like Jun 25th – 28th 2018 - that is, with the Month part always displaying on the start date.

Where the months differ, it should display like Jun 25th – Jul 28th 2018.

The issue stems from the current logic which assumes that the end date format stays consistent, and only the start date is consolidated.

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.