PHP code example of lawondyss / moment-php

1. Go to this page and download the library: Download lawondyss/moment-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lawondyss / moment-php example snippets


$moment = new MomentPHP\MomentPHP();

$string = '1980-12-07';
$string = '7 December 1980';
$string = '+1 week 2 days 4 hours 2 seconds';
$string = 'next Thursday';

$moment = new MomentPHP\MomentPHP($string);

$integer = 345061302;
$integer = time();

$moment = new MomentPHP\MomentPHP($integer);

$moment = new MomentPHP\MomentPHP(new DateTime());

$string = '1980-07-12';
$format = 'Y-d-m';

$moment = new MomentPHP\MomentPHP($string, $format);

$string = '1980-12-07';
$format = array('U', 'Y-m-d');

$moment = new MomentPHP\MomentPHP($string, $format);

$timezone = 'Europe/Prague';
$timezone = new DateTimeZone('Europe/Prague');

$moment = new MomentPHP\MomentPHP(null, null, $timezone);

$moment = new MomentPHP\MomentPHP('1980-12-07 19:21:42', null, 'Europe/Prague');

var_dump( $moment->format('d/m/Y') ); // string(10) "07/12/1980"

var_dump( $moment->timestamp() ); // int(345061302)

var_dump( $moment->seconds() ); // string(2) "42"
// it`s same as
var_dump( $moment->second() );

var_dump( $moment->minutes() ); // string(2) "21"
// it's same as
var_dump( $moment->minute() );

var_dump( $moment->hours() ); // string(2) "19"
// it's same as
var_dump( $moment->hour() );

var_dump( $moment->days() ); // string(2) "07"
// it's same as
var_dump( $moment->day() );

var_dump( $moment->months() ); // string(2) "12"
// it's same as
var_dump( $moment->month() );

var_dump( $moment->years ); // string(4) "1980"
// ti's same as
var_dump( $moment->year() );

var_dump( $moment->weeks() ); //string(2) "49"
// it's same as
var_dump( $moment->week() );

// 1 (for Monday) through 7 (for Sunday)
var_dump( $moment->dayOfWeek() ); // string(1) "7"

// starting from 1
var_dump( $moment->dayOfYear() ); // string(3) "342"

var_dump( $moment->nameOfDayShort() ); // string(3) "Sun"

var_dump( $moment->nameOfDayLong() ); // string(6) "Sunday"

var_dump( $moment->dayWithSuffix() ); // string(3) "7th"

var_dump( $moment->daysInMonth() ); // string(2) "31"

var_dump( $moment->nameOfMonthShort() ); // string(3) "Dec"

var_dump( $moment->nameOfMonthLong() ); // string(8) "December"

var_dump( $moment->hourWithSuffix() ); // string(3) "7PM"

var_dump( $moment->isoDate() ); // string(25) "1980-12-07T19:21:42+01:00"

var_dump( $moment->nameOfTimezone() ); // string(13) "Europe/Prague"

var_dump( $moment->timezoneOffset() ); // int(3600)

$momentB = new MomentPHP('2000-01-01 00:00:00', 'Y-m-d H:i:s', 'Europe/Prague');
var_dump( $momentB->diff($moment) ); // int(601619898)

var_dump( $momentB->diff($moment, 'months') ); // int(227)

var_dump( $momentB->diff($moment, 'months', true) ); // double(227.81)

$momentA = new MomentPHP('1980-12-07');
$momentB = new MomentPHP('1980-12-08');

var_dump( $momentA->from($momentB) ); // string(8) "in a day"
var_dump( $momentB->from($momentA) ); // string(9) "a day ago"

$momentA = new MomentPHP('1980-12-07');
$momentB = new MomentPHP('1980-12-08');

var_dump( $momentA->from($momentB, true) ); // string(5) "a day"
var_dump( $momentB->from($momentA, true) ); // string(5) "a day"

$moment = new MomentPHP;
$moment->add(1, 'day');

var_dump( $moment->fromNow() ); // string(8) "in a day"

$number = 1;
$unit = 'day';
var_dump( $moment->add($number, $unit)->days() ); // string(2) "08"

$interval = DateInterval('1 day');
var_dump( $moment->add($interval)->days() ); // string(2) "08"

$fields = array('days' => 1, 'years' => 1);
var_dump( $moment->add($fields)->format('d|Y') ); // string(7) "08|1981"

$number = 1;
$unit = 'day';
var_dump( $moment->sub($number, $unit)->days() ); // string(2) "06"

var_dump( $moment->isoDate() ); // string(25) "1980-12-07T19:21:42+01:00"

var_dump( $moment->startOf('minutes')->isoDate() ); // string(25) "1980-12-07T19:21:00+01:00"
var_dump( $moment->startOf('hours')->isoDate() ); // string(25) "1980-12-07T19:00:00+01:00"
var_dump( $moment->startOf('days')->isoDate() ); // string(25) "1980-12-07T00:00:00+01:00"
var_dump( $moment->startOf('months')->isoDate() ); // string(25) "1980-12-01T00:00:00+01:00"
var_dump( $moment->startOf('years')->isoDate() ); // string(25) "1980-01-01T00:00:00+01:00"

var_dump( $moment->isoDate() ); // string(25) "1980-12-07T19:21:42+00:00"

var_dump( $moment->endOf('minutes')->isoDate() ); // string(25) "1980-12-07T19:21:59+00:00"
var_dump( $moment->endOf('hours')->isoDate() ); // string(25) "1980-12-07T19:59:59+00:00"
var_dump( $moment->endOf('days')->isoDate() ); // string(25) "1980-12-07T23:59:59+00:00"
var_dump( $moment->endOf('months')->isoDate() ); // string(25) "1980-12-31T23:59:59+00:00"
var_dump( $moment->endOf('years')->isoDate() ); // string(25) "1980-12-31T23:59:59+00:00"

$moment = new MomentPHP\MomentPHP('2012', 'Y');
var_dump( $moment->isLeapYear() ); // bool(true)

$moment = new MomentPHP\MomentPHP('2013', 'Y');
var_dump( $moment->isLeapYear() ); // bool(false)

$moment = new MomentPHP\MomentPHP('06', 'm');
var_dump( $moment->isDST() ); // bool(true)

$moment = new MomentPHP\MomentPHP('12', 'm');
var_dump( $moment->isDST() ); // bool(false)

var_dump( $moment->isBefore('1980-12-31') ); // bool(true)

var_dump( $moment->isBefore('1980-12-31', 'months') ); // bool(false)

var_dump( $moment->isSame('1980-12-07') ); // bool(false) because time for compare date is "00:00:00"
var_dump( $moment->startOf('days')->isSame() ); // bool(true) because time for origin date set on "00:00:00"

var_dump( $moment->isMomentPHP(new MomentPHP\MomentPHP) ); // bool(true)
var_dump( $moment->isMomentPHP(new DateTime) ); // bool(false)
json
{
    "awondyss/moment-php": "dev-master"
    }
}
sh
php composer.phar install