Giter VIP home page Giter VIP logo

laravel-date's Introduction

Date for LaravelPHP

Working w/ dates can be a bear. While this helper class probably is missing some things, I find it helpful.

Install

In application/bundles.php add:

'date' => array('auto' => true),

Examples

Some basic examples:

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

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

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

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

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

// get a predefined format
$date = Date::forge('last sunday')->format('datetime'); // 2012-04-08 00:00:00
$date = Date::forge('last sunday')->format('date'); // 2012-04-08
$date = Date::forge('last sunday')->format('time'); // 00:00:00

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

// get relative 'ago' format
$date = Date::forge('now - 10 minutes')->ago() // 10 minutes ago

Math w/ Dates

Let's look at some date comparison examples:

// passing objects
$date1 = Date::forge('2012-04-05');
$date2 = Date::forge('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
)
*/

Formatting

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

Notes

  • The class relies on strtotime() to make sense of your strings, and strftime() to make the format changes. Just always check the $date->time() output to see if you get false timestamps... which means the class couldn't understand what you were telling it.
  • This class previously would not work on Windows machines. With the help of some contributors, I believe this now works properly on both WIN and UNIX machines.

laravel-date's People

Contributors

swt83 avatar eduardocruz avatar

Watchers

Endel Dreyer avatar James Cloos avatar  avatar

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.