Giter Club home page Giter Club logo

php-date's Introduction

Date

A PHP library for working w/ dates.

Install

Normal install via Composer.

Usage

Some basic examples:

use Travis\Date;

// default timestamp is now
$date = Date::make();

// pass timestamps
$date = Date::make(1333857600);

// pass strings
$date = Date::make('last sunday');

// pass objects
$date = Date::make('last sunday');
$new_date = Date::make($date);

// get timestamp
$date = Date::make('last sunday')->time(); // 1333857600

// get a nice format
$date = Date::make('last sunday')->format('%B %d, %Y'); // April 08, 2012

// amend the timestamp value, relative to existing value
$date = Date::make('2012-04-05')->remake('+ 3 days')->format('%F'); // 2012-04-08

// amend the timestamp value, and keep original date object
$date = Date::make('2012-04-05');
$new_date = $date->remake('+3 days', true); // flag to return modified cloned object
echo $date->format('%F'); // 2013-04-05
echo $new_date->format('%F'); // 2014-04-08

// get relative 'ago' format
echo Date::make('now - 10 minutes')->ago() // 10 minutes ago

Math w/ Dates

Let's look at some date comparison examples:

// passing objects
$date1 = Date::make('2012-04-05');
$date2 = Date::make('2012-04-08');
$diff = Date::diff($date1, $date2);
/*
DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 3
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 3
)
*/

// passing timestamps
$diff = Date::diff(1333598400, 1333857600);
/*
DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 3
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 3
)
*/

// passing strings
$diff = Date::diff('April 08, 2012', 'April 05, 2012');
/*
DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 3
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 1
    [days] => 3
)
*/

Drawing Calendars

You can print a nice HTML table of a calendar:

$html = Date::draw_calendar($month, $year); // both params should be integers

You can print custom content inside the cells for a specific date by passing a closure:

$html = Date::draw_calendar($month, $year, function($date) use ($my_custom_param) {
    if ($date->format('%F') == $my_custom_param)
    {
        echo 'something special';
    }
});

Take a look at the view used in the package and you'll see what CSS options are available.

Notes

For help in building your formats, checkout the PHP strftime() docs.

php-date's People

Contributors

eduardocruz avatar swt83 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

php-date's Issues

Add Localization

My System is in brazilian portuguese and english. Thus, I forked the project and translated the "ago, day, month, year ..." messages. It is pretty simple, but it might be helpful to someone else.

I will make a pull request for that.

Ago method

Ago method (from css-tricks) gives me false result, for example "1 weeks ago". I'm not native english but I think the correct value should be "1 week ago".

Port to Laravel 4

You should port your library to a composer package that is compatible with Laravel 4!

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.