Coder Social home page Coder Social logo

fightbulc / moment.php Goto Github PK

View Code? Open in Web Editor NEW
968.0 34.0 154.0 616 KB

Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js

License: Other

PHP 96.88% HTML 3.12%
date time i18n internationalization formatter moment php calendar parsing

moment.php's Introduction

Support

I am a dad now for the last 1,5 years and that clearly shows in being on time with merging PRs or pushing this package further. Time is the biggest constraint here. I would be happy to pass the project on to somebody who has more time and the motivation to push the project forward. Just drop me a message. Cheers!

                                      _           _
 _ __ ___   ___  _ __ ___   ___ _ __ | |_   _ __ | |__  _ __
| '_ ` _ \ / _ \| '_ ` _ \ / _ \ '_ \| __| | '_ \| '_ \| '_ \
| | | | | | (_) | | | | | |  __/ | | | |_ _| |_) | | | | |_) |
|_| |_| |_|\___/|_| |_| |_|\___|_| |_|\__(_) .__/|_| |_| .__/
                                           |_|         |_|

Integrate

Latest Stable Version Total Downloads Monthly Downloads

Change log

Intro

What is moment.php?

Date library for parsing, manipulating and formatting dates w/ i18n.

Any dependencies?

PHP 5.3 or later since moment.php is based on php's DateTime Class.


Install

Easy install via composer. Still no idea what composer is? Inform yourself here.

composer require fightbulc/moment

Quick examples

Get a moment

$m = new \Moment\Moment(); // default is "now" UTC
echo $m->format(); // e.g. 2012-10-03T10:00:00+0000

$m = new \Moment\Moment('now', 'Europe/Berlin');
echo $m->format(); // e.g. 2012-10-03T12:00:00+0200

$m = new \Moment\Moment('2017-06-06T10:00:00', 'Europe/Berlin');
echo $m->format(); // e.g. 2012-10-03T12:00:00+0200

$m = new \Moment\Moment(1499366585);
echo $m->format(); // e.g. 2017-07-06T18:43:05+0000

Accepted date formats

Moment parses the following date formats as input:

const ATOM = 'Y-m-d\TH:i:sP'; // 2005-08-15T15:52:01+00:00
const COOKIE = 'l, d-M-y H:i:s T'; // Monday, 15-Aug-2005 15:52:01 UTC
const ISO8601 = 'Y-m-d\TH:i:sO'; // 2005-08-15T15:52:01+0000
const RFC822 = 'D, d M y H:i:s O'; // Mon, 15 Aug 05 15:52:01 +0000
const RFC850 = 'l, d-M-y H:i:s T'; // Monday, 15-Aug-05 15:52:01 UTC
const RFC1036 = 'D, d M y H:i:s O'; // Mon, 15 Aug 05 15:52:01 +0000
const RFC1123 = 'D, d M Y H:i:s O'; // Mon, 15 Aug 2005 15:52:01 +0000
const RFC2822 = 'D, d M Y H:i:s O'; // Mon, 15 Aug 2005 15:52:01 +0000
const RSS = 'D, d M Y H:i:s O'; // Mon, 15 Aug 2005 15:52:01 +0000
const W3C = 'Y-m-d\TH:i:sP'; // 2005-08-15T15:52:01+00:00

// Moment also tries to parse dates without timezone or without seconds

const NO_TZ_MYSQL = 'Y-m-d H:i:s'; // 2005-08-15 15:52:01
const NO_TZ_NO_SECS = 'Y-m-d H:i'; // 2005-08-15 15:52
const NO_TIME = 'Y-m-d'; // 2005-08-15

// time fractions ".000" will be automatically removed
$timeWithFraction = '2016-05-04T10:00:00.000';

Switch locale

Have a look at the Locales folder to see all supported languages. Default locale is en_GB.

$m = new \Moment\Moment();
echo $m->format('[Weekday:] l'); // e.g. Weekday: Wednesday

// set german locale
\Moment\Moment::setLocale('de_DE');

$m = new \Moment\Moment();
echo $m->format('[Wochentag:] l'); // e.g. Wochentag: Mittwoch

Supported languages so far:

ar_TN Arabic (Tunisia) ca_ES Catalan cs_CZ Czech da_DK Danish de_DE German (Germany) en_CA English (Canada) en_GB English (British) en_US English (American) eo_EO Esperanto es_ES Spanish (Europe) fa_IR Farsi fi_FI Finnish fr_CA French (Canada) fr_FR French (Europe) hu_HU Hungarian id_ID Indonesian it_IT Italian ja_JP Japanese kz_KZ Kazakh lv_LV Latvian (Latviešu) nl_NL Dutch oc_LNC Lengadocian pl_PL Polish pt_BR Portuguese (Brazil) pt_PT Portuguese (Portugal) ru_RU Russian (Basic version) sv_SE Swedish th_TH Thai tr_TR Turkish uk_UA Ukrainian vi_VN Vietnamese zh_CN Chinese zh_TW Chinese (traditional)


Switch timezones

$m = new \Moment\Moment('2012-04-25T03:00:00', 'CET');
echo $m->setTimezone('UTC')->format(); // 2012-04-25T01:00:00+0000

Change default timezone

\Moment\Moment::setDefaultTimezone('CET');

$m = new \Moment\Moment('2016-09-13T14:32:06');
echo $m->format(); // 2016-09-13T14:32:06+0100

Custom format

I. PHP only (Standard)

$m = new \Moment\Moment('2012-04-25T03:00:00', 'CET');
echo $m->format('l, dS F Y / H:i (e)'); // Wednesday, 25th April 2012 / 03:00 (Europe/Berlin)

Formats are based on PHP's Date function and DateTime class.

II. Non-php formats

You can now inject different format handling by passing along a class which implements the FormatsInterface. You can find an example within the test folder for implementing all formats from moment.js. Thanks to Ashish for taking the time to match moment.js formats to those of PHP. Have a look at the test script to see the example in action.

Everybody can write format classes in the same manner. Its easy and scalable.

// get  desired formats class
// create a moment
$m = new \Moment\Moment('2012-04-25T03:00:00', 'CET');

// format with moment.js definitions
echo $m->format('LLLL', new \Moment\CustomFormats\MomentJs()); // Wednesday, April 25th 2012 3:00 AM

Custom formats can also come as part of every Locale. If it does not exist for your locale yet go ahead and add it. See an example for the French locale.

III. Easy text escaping

Just wrap all your text within [] and all characters will be automatically escaped for you.

$m = new \Moment\Moment('2012-04-25T03:00:00', 'CET');
echo $m->format('[We are in the month of:] F'); // We are in the month of: April

IV. Fixed ordinal representations

PHP's interal ordinal calculation seems to be buggy. I added a quick fix to handle this issue.

The following example prints the week of the year of the given date. It should print 22nd:

// internal function
date('WS', mktime(12, 22, 0, 5, 27, 2014)); // 22th

// moment.php
$m = new \Moment\Moment('2014-05-27T12:22:00', 'CET');
$m->format('WS'); // 22nd

Create custom moments and manipulate it

I. Past/Future moments

$m = new \Moment\Moment('2012-05-15T12:30:00', 'CET');
echo $m->addHours(2)->format(); // 2012-05-15T14:30:00+0200

$m = new \Moment\Moment('2012-05-15T12:30:00', 'CET');
echo $m->subtractDays(7)->subtractMinutes(15)->format(); // 2012-05-08T12:15:00+0200

$m = new \Moment\Moment('@1401443979', 'CET'); // unix time
echo $m->subtractDays(7)->subtractMinutes(15)->format(); // 2014-05-23T09:44:39+0000

II. Clone a given moment

Sometimes its useful to take a given moment and work with it without changing the origin. For that use cloning().

$m = new \Moment\Moment('2012-05-15T12:30:00', 'CET');
$c = $m->cloning()->addDays(1);

echo $m->getDay(); // 15
echo $c->getDay(); // 16

Alternately, you can enable immutable mode on the origin.

$m = new \Moment\Moment('2012-05-15T12:30:00', 'CET', true);
$c = $m->addDays(1);

echo $m->getDay(); // 15
echo $c->getDay(); // 16

// You can also change the immutable mode after creation:
$m->setImmutableMode(false)->subtractDays(1);

echo $m->getDay(); // 14

Immutable mode makes all modification methods call cloning() implicitly before applying their modifications.

III. Methods for manipulating the date/time

Add Subtract
addSeconds($s) subtractSeconds($s)
addMinutes($i) subtractMinutes($i)
addHours($h) subtractHours($h)
addDays($d) subtractDays($d)
addWeeks($w) subtractWeeks($w)
addMonths($m) subtractMonths($m)
addYears($y) subtractYears($y)

IV. Setter/Getter

Setter Getter
setSecond($s) getSecond()
setMinute($m) getMinute()
setHour($h) getHour()
setDay($d) getDay()
setMonth($m) getMonth()
setYear($y) getYear()
-- getQuarter()

Difference between dates

$m = new \Moment\Moment('2013-02-01T07:00:00');
$momentFromVo = $m->fromNow();

// or from a specific moment
$m = new \Moment\Moment('2013-02-01T07:00:00');
$momentFromVo = $m->from('2011-09-25T10:00:00');

// result comes as a value object class
echo $momentFromVo->getDirection()  // "future"
echo $momentFromVo->getSeconds()    // -42411600
echo $momentFromVo->getMinutes()    // -706860
echo $momentFromVo->getHours()      // -11781
echo $momentFromVo->getDays()       // -490.88
echo $momentFromVo->getWeeks()      // -70.13
echo $momentFromVo->getMonths()     // -17.53
echo $momentFromVo->getYears()      // -1.42
echo $momentFromVo->getRelative()   // in a year

Get date periods (week, month, quarter)

Sometimes its helpful to get the period boundaries of a given date. For instance in case that today is Wednesday and I need the starting-/end dates from today's week. Allowed periods are week, month and quarter.

$m = new \Moment\Moment('2013-10-23T10:00:00');
$momentPeriodVo = $m->getPeriod('week');

// results comes as well as a value object class
echo $momentPeriodVo
    ->getStartDate()
    ->format('Y-m-d'); // 2013-10-21

echo $momentPeriodVo
    ->getEndDate()
    ->format('Y-m-d'); // 2013-10-27

echo $momentPeriodVo
    ->getRefDate()
    ->format('Y-m-d'); // 2013-10-23

echo $momentPeriodVo->getInterval(); // 43 = week of year

Same procedure for monthly and quarterly periods:

$momentPeriodVo = $m->getPeriod('month');
$momentPeriodVo = $m->getPeriod('quarter');

Calendar Times

Calendar time displays time relative to now, but slightly differently than Moment::fromNow(). Moment::calendar() will format a date with different strings depending on how close to today the date is.

(new \Moment\Moment('2014-03-30T16:58:00', 'CET'))->subtractDays(6)->calendar(); // last week
(new \Moment\Moment('2014-03-30T16:58:00', 'CET'))->subtractDays(1)->calendar(); // yesterday
(new \Moment\Moment('2014-03-30T16:58:00', 'CET'))->calendar(); // today
(new \Moment\Moment('2014-03-30T16:58:00', 'CET'))->addDays(1)->calendar(); // tomorrow
(new \Moment\Moment('2014-03-30T16:58:00', 'CET'))->addDays(3)->calendar(); // next week
(new \Moment\Moment('2014-03-30T16:58:00', 'CET'))->addDays(10)->calendar(); // everything else
Time Display
Last week Last Monday at 15:54
The day before Yesterday at 15:54
The same day Today at 15:54
The next day Tomorrow at 15:54
The next week Wednesday at 15:54
Everything else 04/09/2014

Note: Use $moment->calendar(false) to leave out the time at 00:00.


startOf / endOf

Same process as for moment.js: mutates the original moment by setting it to the start/end of a unit of time.

$m = new \Moment\Moment('20140515T10:15:23', 'CET');

$m->startOf('year');    // set to January 1st, 00:00 this year
$m->startOf('quarter'); // set to the beginning of the current quarter, 1st day of months, 00:00
$m->startOf('month');   // set to the first of this month, 00:00
$m->startOf('week');    // set to the first day of this week, 00:00
$m->startOf('day');     // set to 00:00 today
$m->startOf('hour');    // set to now, but with 0 mins, 0 secs
$m->startOf('minute');  // set to now, but with 0 seconds

$m->endOf('year');    // set to December 31st, 23:59 this year
$m->endOf('quarter'); // set to the end of the current quarter, last day of month, 23:59
$m->endOf('month');   // set to the last of this month, 23:59
$m->endOf('week');    // set to the last day of this week, 23:59
$m->endOf('day');     // set to 23:59 today
$m->endOf('hour');    // set to now, but with 59 mins, 59 secs
$m->endOf('minute');  // set to now, but with 59 seconds

Note: I ignored the period of second since we are not dealing with milliseconds.


Get dates for given weekdays for upcoming weeks

For one of my customers I needed to get moments by selected weekdays. The task was: give me the dates for Tuesdays and Thursdays for the next three weeks. So I added a small handler which does exactly this. As result you will receive an array filled with Moment Objects.

// 1 - 7 = Mon - Sun
$weekdayNumbers = [
    2, // tuesday
    4, // thursday
];

$m = new \Moment\Moment();
$dates = $m->getMomentsByWeekdays($weekdayNumbers, 3);

// $dates = [Moment, Moment, Moment ...]

You can now run through the result and put it formatted into a drop-down field or for whatever you might need it.


Roadmap

  • Try to port useful methods from moment.js
  • Add unit tests

Change Log

Unreleased

  • added:
    • support for PHP 8.2 #218
    • support for PHP 8.3 #227

1.33.0

  • fixed:
    • French locale
    • Canadian tests
  • added:
    • Esperanto locale
    • Kazakh locale

1.32.1

  • fixed:
    • DateTime::createFromFormat signature match

1.32.0

  • added:
    • Farsi locale

1.31.0

  • added:
    • code checks vis GH action workflows
    • Canadian locale for English/French

1.30.1

  • fixed:
    • merge with a couple of PRs (thanks!)

1.30.0

  • added:
    • Finish locale
  • fixed:
    • Swedish locale

1.29.0

  • updated Italian locale
  • added:
    • custom formats for en_US
    • flag for loading similar locale

1.28.3

  • fixed typehint issue

1.28.2

  • fixed:
    • missing relativeTime format
    • allow 9-digit unixtime

1.28.1

  • fixed RFC2822 as valid format

1.28.0

  • fixed relative time
  • added Norwegian locale

1.27.0

1.26.10

  • fixed:
    • Occitan locale

1.26.9

  • fixed:

1.26.8

  • added:
    • Portuguese (pt_PT)

1.26.7

  • fixed:
    • Hungarian locale weekdays order

1.26.6

  • added:
    • allow initialising Moment with unix timestamp without leading @

1.26.5

  • fixed:
    • Fix format of 'LLL' in Custom Formats

1.26.4

  • fixed:
    • removed php5.4+ only syntax

1.26.3

  • fixed:
    • Danish day- and monthnames correct case
    • French locale
    • PHPDocs
  • added:
    • consts for NO_TZ_MYSQL, NO_TZ_NO_SECS and NO_TIME when parsing dates

1.26.2

  • added:
    • Dutch customFormat

1.26.1

  • fixed:
    • Russian locale

1.26.0

  • added:
    • Turkish locale
  • fixed:
    • Lengadocian locale

1.25.1

  • fixed:
    • PHP7.1 setTime requires $microseconds

1.25

  • added:
    • Ukrainian locale

1.24

  • added:
    • Hungarian locale

1.23.1

  • fixed:
    • Lengadocian locale

1.23.0

  • added:
    • Vietnamese locale
    • Lengadocian locale

1.22.0

  • added:
    • Change default timezone
  • fixed:
    • FormatsInterface docs

1.21.0

  • added:
    • Arabic locale
    • Custom format on locale level

1.20.9

  • fixed:
    • Russian locale
  • added:
    • Russian locale tests

1.20.8

  • fixed:
    • Polish locale
    • Calculation of seconds

1.20.7

  • fixed:
    • Russian: more relative time fixes

1.20.6

  • fixed:
    • Russian locale relative time: day handling

1.20.5

  • fixed:
    • missing immutable handling

1.20.4

  • fixed:
    • Improved Polish locale (added Nominativ)

1.20.3

  • fixed:
    • Chinese locale

1.20.2

  • added accepted formats to README

1.20.1

  • fixed:
    • Thai locale

1.20.0

  • added:
    • Catalan locale
  • fixed:
    • Polish locale test

1.19.0

  • added:
    • Russian locale
  • fixed:
    • Polish locale test

1.18.0

  • added:
    • Immutable mode
  • fixed:
    • Polish locale

1.17.0

  • added:
    • Polish locale

1.16.0

  • added:
    • Indonesian locale

1.15.0

  • added:
    • Japanese locale

1.14.1

  • fixed:
    • typo in Dutch locale

1.14.0

  • added:
    • Dutch locale

1.13.0

  • added:
    • Swedish locale

1.12.0

  • added:
    • Danish locale

1.11.4

  • fixed:
    • fixed starting/ending weekday for Romanian locale

1.11.3

  • fixed:
    • adding delimiter character to Italian locale

1.11.1

  • fixed:
    • passing back new instance for startOf/endOf for week, month, quarter

1.11.0

  • added:
    • locale Czech

1.10.4

  • added:
    • calendar locale receives as \Closure the following params function(Moment $m) {}
    • relativeTime locale receives as \Closure the following params function($count, $direction, Moment $m) {}

1.10.3

  • added:
    • fixed passing closures to locale (calendar, relativeTime)
    • set correct german locale information

1.10.2

  • added:
    • fixed Thai locale strings

1.10.1

  • added:
    • locale traditional Chinese

1.10.0

  • added:
    • locale Chinese
    • ordinal formatter receives now the token e.g. the token within dS is d

1.9.1

  • fixed: english ordinal issue for numbers between 11 - 13

1.9.0

  • added: locale Italian

1.8.1

  • fixed: english ordinal issue

1.8.0

  • added: locale Portuguese

1.7.2

  • fixed:
    • Locale displayed wrong month name (#34)
    • Changed the order of weekdays within locale files

1.7.1

  • added:
    • getWeekdayNameLong()
    • getWeekdayNameShort()
    • getMonthNameLong()
    • getMonthNameShort()

1.7.0

  • added:
    • Locale: Thai

1.6.0

  • added:
    • Locale
    • MomentFromVo:
      • getMonths()
      • getYears()
      • getRelative()
  • fixed:
    • MomentFromVo:
      • getSeconds() shows now direction as well

1.5.3

  • fixed:
    • timezone issue which occured only for unixtime dates
  • other:
    • MomentFromVo:
      • direction returns now: "future" (-) / "past" (+)
      • time values are now type casted as floats

1.5.2

  • fixed:
    • unrecognised timezone when constructing a Moment

1.5.1

  • added:
    • getMomentsByWeekdays()
    • getWeekday()
    • getWeekOfYear()
  • other:
    • escaped text

1.5.0

  • added:

    • startOf and endOf as implemented by moment.js
    • get the quarter period of a given date
    • setDay()
    • getDay()
    • setMonth()
    • getMonth()
    • setYear()
    • getYear()
    • getQuarter()
    • setSecond()
    • getSecond()
    • setMinute()
    • getMinute()
    • setHour()
    • getHour()
    • added cloning()
      • create a new mutable moment based of the given instance
    • added getInterval() to MomentPeriodVo to indicate the interval of the given period
      • week = week of the year
      • month = month of the year
      • quarter = quarter of the year
    • added a static class MomentHelper
      • get the period for a given quarter in a given year
    • fixed PHP's internal ordinal calculation (also in combination with moment.js formatting)
      • e.g. WS for 21th week of the year shows now correct 21th etc.
    • you can now escape text by wrapping it in []
      • e.g. [Hello World] will be automatically transformed into \H\e\l\l\o \W\o\r\l\d
  • removed:

    • add()
    • subtract()

1.4.0

  • added:
    • calendar format as implemented by moment.js

1.3.0

  • fixed:

    • incompatibility w/ PHP 5.3
  • added:

    • Exception throw as MomentException
    • Date validation on instantiation:
      • test for dates w/ format YYYY-mm-dd and YYYY-mm-ddTHH:ii:ss
      • throws MomentException on invalid dates
    • addSeconds()
    • addMinutes()
    • addHours()
    • addDays()
    • addWeeks()
    • addMonths()
    • addYears()
    • subtractSeconds()
    • subtractMinutes()
    • subtractHours()
    • subtractDays()
    • subtractWeeks()
    • subtractMonths()
    • subtractYears()
  • deprecated:

    • add()
    • subtract()

Contributing

The maintainers of this project suggest following the contribution guide.

License

Moment.php is freely distributable under the terms of the MIT license.

Copyright (c) 2017 Tino Ehrich

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

moment.php's People

Contributors

akoebbe avatar aliazizi avatar amouro avatar asiby avatar atefbb avatar blacknell avatar bpahan avatar dfitiskin avatar efiware avatar engin avatar fightbulc avatar greew avatar hendrik-sch avatar ioliva avatar jcherniak avatar linmad avatar localheinz avatar maximevaly avatar morgnus avatar oscarwodan avatar petrofcikmatus avatar popinz-api avatar ptica avatar quent-in avatar senorsen avatar sensetivity avatar wulff avatar ydatech avatar yosmanyga avatar zaszczyk 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

moment.php's Issues

Calendar() issue after 0 o'clock

Hi,

I'm using moment.php in my application. If someone posts something at 19:00 o'clock, calendar() will still output "Heute um 19:10" (Today at 19:10), even when its after > 0 o'clock. After 0 it should output "yesterday at 19:10"

'S' is the ordinal suffix for day-of-the-month only.

In MomentJs.php you have listed conversions for the following momentJS format strings to their appropriate php format strings:

Moment.js PHP Example (2014-01-02)
Mo nS 1nd ✖
Do jS 2nd ✓
DDDo zS 1nd ✖
do wS 4nd ✖
wo WS 1nd ✖
Wo WS 1nd ✖

The only one of the above conversions that is correct is the 'Do' -> 'jS' conversion. According to the PHP docs, 'S' is the English ordinal suffix for the day of the month only.

Sources:
http://php.net/manual/en/function.date.php

getDays correct behaviour

I want to calculate how many days have passed from event - between "yesterday" and "now".
What is correct answer?
It is -> one day!
Agreed?

So, let's calculate it! I've got some results, that doesn't correspond to correct interval
$m = new \Moment\Moment('2014-11-20 23:59:59'); //yesterday
echo $m->from('2014-11-21 00:00:01')->getDays(); //0 - the same as yesterday?? of course no!
echo $m->from('2014-11-21 23:59:59')->getDays(); //1 - correct, but "lucky" )

I've got the answer - this lib calculates differens in seconds, and then scale it to days.
But how to get correct "human" results in my case?

Extended date format ISO8601 validation - feature

Hi again,
I'm wondering why Moment accepts ISO8601 format in limited way, only YYYY-MM-DDTHH:mm:ss?
It would be more useful if Moment would accept full ISO8601. DateTime accepts all ISO formats,
I investigated a bit code and found that isValid method is responsible for that.
I have rewrited it and now it works as I would like it to do. If you find it interesting , you can use it.

protected function isValidDate()
    {
        $rawDateTime = $this->getRawDateTimeString();

        if (strpos($rawDateTime, '-') === false) {
            return true;
        }

        // ----------------------------------
        // time with indicator "T"
        if (strpos($rawDateTime, 'T') !== false) {
            //We remove fraction if any ... DateTime holds only seconds
            $rawDateTime = preg_replace('/\.[0-9][0-9][0-9]/', '', $rawDateTime);
            $rawTimeZone = substr($rawDateTime, 19);
            if ($rawTimeZone && strpos($rawTimeZone, '+') !== FALSE) {
                if (substr_count($rawTimeZone, ':') > 0) {
                    $momentDateTime = $this->format('Y-m-d\TH:i:sP');
                } else {
                    $momentDateTime = $this->format('Y-m-d\TH:i:sO');
                }
            } elseif ($rawTimeZone) {
                $momentDateTime = $this->format('Y-m-d\TH:i:se');
            } else {
                $momentDateTime = $this->format('Y-m-d\TH:i:s');
            }
        } // time without indicator "T"
        elseif (strpos($rawDateTime, ':') !== false) {
            if (substr_count($rawDateTime, ':') === 2) { // with seconds
                $momentDateTime = $this->format('Y-m-d H:i:s');
            } else {
                $momentDateTime = $this->format('Y-m-d H:i');
            }
        } // without time
        else {
            $momentDateTime = $this->format('Y-m-d');
        }

        return $rawDateTime === $momentDateTime;
    }

and tests

    public function testMoment(){
        $data = '1923-12-31 12:30:00';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

        $data = '1923-12-31T12:30:00.000';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

        $data = '1923-12-31T12:30:00.123';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

        $data = '1923-12-31T12:30:00.123+02:00';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0200', $m->format());

        $data = '1923-12-31T12:30:00.123+0200';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0200', $m->format());

        $data = '1923-12-31T12:30:00.123Z';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

        $data = '1923-12-31T12:30:00.123Europe/Warsaw';
        $m = new Moment($data);
        $this->assertEquals('1923-12-31T12:30:00+0100', $m->format());

        $data = '1923-12-31T12:30:00.123Europe/Warsaw';
        $m = new Moment($data,'UTC');
        $this->assertEquals('1923-12-31T12:30:00+0100', $m->format());

        $data = '1923-12-31T12:30:00.123UTC';
        $m = new Moment($data,'Europe/Warsaw');
        $this->assertEquals('1923-12-31T12:30:00+0000', $m->format());

        $this->tester->printVar($m->format());
        //$this->assertEquals('1923-12-31T12:30:Z', $m->format());

    }

Best regards,
Jarek

ISO8601 includes the mysql datetime formate as well?

Hi,
from the README it looks like moment.php parses only the datetimes with the T time separator,
and as there is no option how to parse custom formatted strings(?) I've originally thought that I could not use this nice library.

But it turns out that the mysql 'Y-m-d H:i:s' is perfectly ok for the constructor after all, maybe a example in the README could assure others to try this library as well!

Doesn't work on PHP 5.3.* - requires PHP >= 5.4

I have tested latest version (1.6.3) on PHP 5.3.29:

PHP 5.3.29 (cli) (built: Aug 14 2014 14:15:02)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies

And it gives parse errors:

PHP Parse error:  syntax error, unexpected '[', expecting ')' in /var/www/smarkio.dev/lib/vendor/fightbulc/moment/src/Moment.php on line 657

This is because [ ] notation of arrays is being used which was introduced only on PHP 5.4 - http://php.net/manual/en/language.types.array.php

(...)

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

getRelative() doesn't state minutes correctly.

$fiveMinutesAgo = time() - (60 * 5);
$m = new Moment('@' . $fiveMinutesAgo);
echo $m->fromNow()->getRelative();
echo $m->fromNow()->getMinutes();
echo $m->fromNow()->getSeconds();
// a few seconds ago
// 5
// 300
  • Same issue when in future.
  • When it gets to the 45+ range it switches to "an hour ago - 46"

I fixed this by changing: https://github.com/fightbulc/moment.php/blob/master/src/MomentFromVo.php#L238 to relative label "mm":

$time = MomentLocale::renderLocaleString(['relativeTime', 'mm'], [round(abs($this->getMinutes()))]);

setTime function needs microseconds in PHP 7.1

Hi.
We are getting PHP 7.1 today, and using moment.php.
But moment.php's setTime function doesn't work now, and an error appears as below.

ErrorException: Declaration of Moment\Moment::setTime($hour, $minute, $second = NULL) should be compatible with DateTime::setTime($hour, $minute, $second = NULL, $microseconds = NULL)

It wants microseconds as argument for PHP 7.1, doesn't it?

I wish it will work.

Composer issue

Getting following error message:

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - The requested package vendorr/moment.php could not be found in any version, there may be a typo in the package name.

Potential causes:
 - A typo in the package name
 - The package is not available in a stable-enough version according to your minimum-stability setting
   see <https://groups.google.com/d/topic/composer-dev/_g3ASeIFlrc/discussion> for more details.

Any ideas how to fix it?

Calendar() feature request

Hi @fightbulc,

I'm building something similar like the facebook timeline. It would be nice if you could add an optional feature for the calendar. I would like to have the ability to set an optional feature for "today". Instead of showing "today at xxx" I'd like to have "1 hour ago, 2 hours ago" till 8 hours ago, after this, when not > 0 o'clock, showing today at XXX.

I think this would be a very helpful and nice feature request. What you think?

Does not appear to allow you to localize "LT"

It appears that LT is fixed to include AM/PM with A in MomentJS.php. LTS also appears to be missing. L is also hardcoded with /

In Moment.js you can define the L's format in the locale via longDateFormat.

subtractMonths not working at the last day of the month

If i give a timestamp of 30th October everything works fine, but if i provide 31st October time then the subtractMonth fails and i get the current month time.

$moment = new \Moment\Moment();
$moment->setTimestamp(1414750126); // optional 
$moment->subtractMonths(1);
$moment->startOf('month')->format(); //2014-10-01T00:00:00+0000 
$moment->endOf('month')->format(); //2014-10-31T23:59:59+0000

Anything between the unixtime 1412121600 and 1414799999 causes this error

php version 5.4.26

How can i change the timezone?

I need an other timezone than UTC. Is it possible to change the default timezone? It's very error-prone if I should need to change the timezone for every moment manually.

Wrong name of the month

Hi @fightbulc,

I have following string:

$message['createdate'] = "2015-06-14 20:46:22"; // from MySQL
$messages[$key]['date'] = (new \Moment\Moment($message['createdate'], 'Europe/Berlin'))->format('d. F');

// Should return 14. Juni

Should return "14. Juni", but it return "14. Juli".

I'll update to the newest version now, but I guess the bug is somewhere else.

Where is NO_TZ_MYSQL?

What has happened to NO_TZ_MYSQL constant? The only place where I can find it is readme file.

Fatal error Locale string does not exist for key: months > 12

Hi @fightbulc

I did nothing - it just looks like moment.php does not like January.

Fatal error: Uncaught exception 'Moment\MomentException' with message 'Locale string does not exist for key: months > 12' in .../vendor/fightbulc/moment/src/MomentLocale.php:77 

Stack trace: 
#0 .../vendor/fightbulc/moment/src/MomentLocale.php(95): Moment\MomentLocale::getLocaleString(Array) 

#1 .../vendor/vendor/fightbulc/moment/src/MomentLocale.php(155): Moment\MomentLocale::renderLocaleString(Array) 

#2 .../vendor/fightbulc/moment/src/Moment.php(149): Moment\MomentLocale::renderSpecialLocaleTags('03. 12__0001') 

#3 .../class.Chat.inc.php(668): Moment\Moment->format('d. F') 

#4 .../vendor/fightbulc/moment/src/MomentLocale.php on line 77

createFromFormat() returns DateTime

Hello,

Moment::createFromFormat() method is still returning a DateTime (not a Moment object). I cannot rely on the __construct() method to guess the correct date, since dates like 03-07-2016 may mean both July 3rd and March 7th. Moment.js accepts a format string in the constructor, maybe Moment could do the same?

Thanks in advance!

Locale ['week']['dow'] is not being honored

I saw that the en_US locale didn't have Sunday as the first day of the week. I put $locale['week']['dow'] = 0; (tried 7 as well), but that didn't seem to affect startOf("week"). I'm not sure that locale setting is being taken in to consideration in the getPeriod('week') case.

Occitan format

In the file oc_LNC.php
on line 17, the hours should be shown like so:
7:45 => 7o45
How can I add the "o" without breaking everything?

Declaration of Moment\Moment::setTime

ErrorException in Moment.php line 1258:
Declaration of Moment\Moment::setTime($hour, $minute, $second = NULL) should be compatible with DateTime::setTime($hour, $minute, $second = NULL, $microseconds = NULL)

Missing minutes in "fromToSeconds"?

Hi,

Seems that minutes are missing.

    protected function fromToSeconds(\DateInterval $dateInterval)
    {
        return ($dateInterval->y * 365 * 24 * 60 * 60) 
+ ($dateInterval->m * 30 * 24 * 60 * 60) 
+ ($dateInterval->d * 24 * 60 * 60) 
+ ($dateInterval->h * 60 * 60) 
+ $dateInterval->s;
    }

below works ok.

protected function fromToSeconds(\DateInterval $dateInterval)
    {
        return ($dateInterval->y * 365 * 24 * 60 * 60) 
+ ($dateInterval->m * 30 * 24 * 60 * 60)
+ ($dateInterval->d * 24 * 60 * 60) 
+ ($dateInterval->h * 60 * 60) 
+ ($dateInterval->i * 60 ) 
+ $dateInterval->s;
    }

Best regards,
Jarek

Please provide own autoloader

Hi @fightbulc,

thanks for this awesome port! Could you please also provide an own AutoLoader instead of relying only to composer? There are sometimes needs to run without. Most of the developers will also ship an own fallback autoload.php in there folders. This would help a lot!

Thank you.

Add support for ru_RU

I want to use this bundle for my project, but I need support for Russian.

I am currently investigating adding support for that locale.

Is anyone else interested in helping?

Ordinal quickfix doesn't handle all cases (noted for en_US, en_GB)

Is the quickfix described in https://github.com/fightbulc/moment.php#iv-fixed-ordinal-representations only for 'WS', or is it supposed to work for other formatting as well?

$result = (new Moment('2015-03-31T20:00:00+0000'))->format('l, dS F Y');

Expected result: Tuesday, 31st March 2015
Actual result: Tuesday, 31rd March 2015

I believe it's recommended to not use ordinals in dates like "March 31, 2015" for en_US, but this still seems like an annoying bug.

Try to discover default timezone from system settings

I think it is better to set default timezone from date_default_timezone_get() or \DateTime->getTimezone()->getName() instead of forcing UTC.
Current behavior may lead to incorrect dates if someone forget to change this.

Foreign language / andere Sprache

Hey,

could you tell me how I could force to get the names (month, day, etc) in german?
setlocale seems not to work.

Hi, kannst du mir verraten wie ich die Wochentage etc in deutsch bekomme? Auch würde ich lieber "gestern, heute, morgen" etc im calender() ausgeben lassen, aktuell kommt alles englisch...

Danke!

French translation

Hello,

There is just a little error for french translation. In fr_FR.php, the calendar['withTime'] should be "[à] H:i" and not [á] H:i.

Thanks to you.

Add "monthsNominative" definition to all locales or fall back to "months"

Currently only a few locales have "monthsNominative" defined. And if "f" format character is used, this causes an exception.

I understand many languages have the same spelling for standalone month name and month name as part of date. For those languages definining separate "months" and "monthNominative" seems to be redundant. But it needs to be defined or the library should use "months" definition if "monthsNominative" is not available, otherwise the same format may be valid for one languages, but not for other.

cloning when changing Moment

addXXX or other methods in Moment which change the object itself always requires cloning to make sure that the original object unchanged. It would be convenient that calling cloning everytime when it is necessary.

Hi there, here it is the it_IT

Hi,
i just want to help you build this repo and since i didn't know how to be a contributor or to make a pull request, here it is the it_IT file:

italia italiano (it_IT) // author => Marco Micheli => https=>//github.com/macfighterpilot return array( "months" => explode('_', 'Gennaio_Febbraio_Marzo_Aprile_Maggio_Giugno_Luglio_Agosto_Settembre_Ottobre_Novembre_Dicembre'), "monthsShort" => explode('_', 'Gen_Feb_Mar_Apr_Mag_Giu_Lug_Ago_Set_Ott_Nov_Dic'), "weekdays" => explode('_', 'Lunedì_Martedì_Mercoledì_Giovedì_Venerdì_Sabato_Domenica'), "weekdaysShort" => explode('_', 'Lun_Mar_Mer_Gio_Ven_Sab_Dom'), "calendar" => array( "sameDay" => '[Oggi]', "nextDay" => '[Domani]', "lastDay" => '[Ieri]', "lastWeek" => '[Scorsa] l', "sameElse" => 'l', "withTime" => '[alle] H:i', "default" => 'd/m/Y', ), "relativeTime" => array( "future" => 'tra %s', "past" => '%s fa', "s" => 'pochi secondi', "m" => 'un minuto', "mm" => '%d minuti', "h" => 'una ora', "hh" => '%d ore', "d" => 'un giorno', "dd" => '%d giorni', "M" => 'un mese', "MM" => '%d mesi', "y" => 'un anno', "yy" => '%d anni', ), "ordinal" => function ($number) { return $number . ''; }, "week" => array( "dow" => 1, // Monday is the first day of the week. "doy" => 4 // The week that contains Jan 4th is the first week of the year. ), );

startOf and endOf don't seem to set the value for week, quarter, and month

I was trying to do $date1->startOf('week'); and I was expecting $date1 to change to the first day of the week, but it didn't. After looking at the code I see that minute, hour, day, and year all use setTime or setDate changing the state of the object, but week, quarter, and month only return a new Moment and don't adjust the current instance. This is the same for endOf. Is this by design?

seem wrong with me

now time is :

php -r "echo date('Y-m-d H:i:s'),PHP_EOL;"

2017-02-15 09:52:42

php -r "echo time(),PHP_EOL;" 

1487123624

here is the code :

require  __DIR__."/vendor/autoload.php";
$m = new \Moment\Moment("2017-02-21 09:40:22", 'PRC');
\Moment\Moment::setLocale('zh_CN');
echo $m->calendar(),PHP_EOL;

result is:
本周二 09:40
but the result should be "下周二",please have a look,thanks!

Textual representation of weekdays in Hungarian locale are off by 1 day

When I format a date in Hungarian, the textual representation of the weekday is wrong. For Thursday 13th of July 2017, the Hungarian format echo's szerda (Wedsnesday).

I think the problem lies in the Hungarian (hu_HU) locale config. It says the week starts at monday, but the 'weekdays' config starts at 'vasárnap' (sunday).

Could be anything else, but it seems like that is the problem.

error with php 7.1

ErrorException in Moment.php line 0:
Declaration of Moment\Moment::setTime($hour, $minute, $second = NULL) should be compatible with DateTime::setTime($hour, $minute, $second = NULL, $microseconds = NULL)

End of month method doesn't consider leap year.

Hi, I am working with this library for my product. It is awesome.
Now, I found an issue with leap year.

This is a test code.

$now = new \Moment\Moment('2016-01-31', 'Asia/Tokyo');
echo $now->subtractDays(1)->addDays(1)->startOf('day')->cloning()
->startOf('month')->addMonths(1)->setDay('30')->subtractMonths(1)->endOf('month')->format();
//It shows "2016-02-28T23:59:59+0000"
//expected is "2016-02-29T23:59:59+0000"

This will only happen when I set TimeZone at first.
If I didn't set TimeZone at first, it will not happen.

I used hack like this.

$now = new \Moment\Moment('2016-01-31', 'Asia/Tokyo');
echo $now->subtractDays(1)->addDays(1)->startOf('day')->cloning()
->startOf('month')->addMonths(1)->setDay('30')->subtractMonths(1)->endOf('month')->endOf('month')->format();
//call endOf('month') twice.
//It shows "2016-02-29T23:59:59+0000"

Thanks.

Implement clone()

It's useful when creating multiple days related to each other especially day end/start, hourly data etc. Example:

$current = new \Moment\Moment('now', 'EET');
$start = $current->clone()->startOf('hour');
$end = $current->clone()->endOf('hour');

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.