Git Product home page Git Product logo

dater's Introduction

Dater

Author GitHub release Software License Packagist

This library can be very helpful to standardize date-time formats in your project & get done easy with different clients timezones.

Install

The recommended way to install Dater is through Composer. You can see package information on Packagist.

{
	"require": {
		"dater/dater": "^2.0"
	}
}

Features

For all features using examples see /example/index.php. PHP > 5.3 required.

Timestamp/datetime input formats support

$dater = new Dater\Dater(new Dater\Locale\En());
echo $dater->datetime();
echo $dater->datetime(time());
echo $dater->datetime(date('Y-m-d H:i:s'));

Formats binding

$dater->format(time(), 'd/m/Y'); // 2013/03/14
$dater->setFormat('slashedDate', 'd/m/Y');
$dater->format(time(), 'slashedDate'); // 2013/03/14
$dater->slashedDate(time()); // 2013/03/14

Format options

All date() format options available and can be overrided or extended:

$dater->addFormatOption('ago', function (DateTime $datetime) {
	return floor((time() - $datetime->getTimestamp()) / 86400) . ' days ago';
});
$dater->format(time() - 60*60*24*7, 'd F Y, ago'); // 14 March 2013, 7 days ago

Locales support

$dater->setLocale(new Dater\Locale\En()); // or you can use Dater\Dater::getLocaleByCode('ru')
echo $dater->date(); // 03/21/2013
echo $dater->now('j F Y'); // 21 March 2013

$dater->setLocale(new Dater\Locale\Ru());
echo $dater->date(); // 21.03.2013
echo $dater->now('j F Y'); // 21 марта 2013

Standard server & user format methods

echo $dater->date(); // 03/21/2013 (client timezone, depends on locale)
echo $dater->time(); // 5:41 AM (client timezone, depends on locale)
echo $dater->datetime(); // 03/21/2013 5:41 (client timezone, depends on locale)
echo $dater->isoDate(); // 2013-03-21 (client timezone)
echo $dater->isoTime(); // 05:41:28 (client timezone)
echo $dater->isoDatetime(); // 2013-03-21 05:41:28 (client timezone)
echo $dater->serverDate(); // 2013-03-21 (server timezone)
echo $dater->serverTime(); // 09:41:28 (server timezone)
echo $dater->serverDatetime(); // 2013-03-21 09:41:28 (server timezone)

Native PHP DateTime class object init & formatting

$datetime = $dater->initDatetimeObject('2013-03-21 08:18:06', 'UTC', 'Europe/London');
$datetime->modify('+10 years');
echo $dater->formatDatetimeObject($datetime, 'date'); // 03/21/2013
// or same thing in one line with Dater\Dater :)
echo $dater->modify('2013-03-21 08:18:06', 'date', 'UTC', 'Europe/London'); // 03/21/2013

Timezones conversion

$dater->setServerTimezone('Europe/Moscow');
$dater->setClientTimezone('Europe/London');
echo $dater->serverDatetime(); // 2013-03-21 08:18:06
echo $dater->isoDatetime(); // 2013-03-21 04:18:06
echo $dater->time(); // 04:18

Timezone auto-detection

Based on JavaScript jsTimezoneDetect library with sending result to server by COOKIE.

$timezoneDetector = new Dater\TimezoneDetector();
echo '<html><head>' . $timezoneDetector->getHtmlJsCode() .'</head></html>'; // <script>...</script>
echo $timezoneDetector->getClientTimezone(); // Europe/London

Convert request datetime to server timezone

Is useful to auto-convert all client request datetime data to server timezone.

$_GET = array('filter' => array('startsFrom' => '2012-12-12 12:00:00'));
$_POST = array('event' => array('starts' => '2012-12-12 12:00:00'));
$_REQUEST = array_merge($_GET, $_POST);
$daterDataHandler = new Dater\DataHandler($dater);
$daterDataHandler->convertRequestDataToServerTimezone(); // all '2012-12-12 12:00:00' replaced to '2012-12-12 10:00:00'

Convert text template datetime timezone

Is useful to auto-convert all datetime in template date to client timezone. For example in Email template body.

$data = 'Timestamp format: 1363238564 (will not be handled)
Timestamp format: 1363238564[Y/m/d]
Timestamp format: 1363238564[datetime]
Server datetime format: 2013-03-14 09:22:44[Y/m/d]
Server datetime format: 2013-03-14 09:22:44[time]
Server datetime format: 2013-03-14 09:22:44';
echo $daterDataHandler->handleDataTimezone($data); 

Will print:

Timestamp format: 1363238564 (will not be handled)
Timestamp format: 2013/03/14
Timestamp format: 14.03.2013 07:22
Server datetime format: 2013/03/14
Server datetime format: 07:22
Server datetime format: 2013-03-14 07:22:44

Convert output datetime to client timezone

$daterDataHandler->enableOutputTimezoneHandler();
echo $data; // $data from previous example will print the same as in prevous example

Recommended

dater's People

Contributors

barbushin avatar cmfcmf avatar dmromanov avatar felixmaier1989 avatar mutator08 avatar nehxby 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  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  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  avatar  avatar  avatar  avatar

Watchers

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

dater's Issues

Merge forked repositories with barbushin/dater

Hi guys,

barbushin/dater has a big refactoting, and I see that there are 5 forks, that are not so up to date. I'm just worried that old version code usage is not compatible with new code, so I want to ask everybody to merge with barbushin/dater repository. It's very easy:

git remote add upstream https://github.com/barbushin/dater.git
git fetch upstream
git merge upstream/master
git pull upstream/master

Small fix to doc is need.

Here is small mistake that makes someone to confuse.

$dater = new Dater\Dater(Dater\Locale\En());

it must be in this view, man

$dater = new Dater\Dater(new Dater\Locale\En());

it will be great if you change it in your description.

addformat

addFormat такого метода нету, вместо него стоит setFormat

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.