PHP code example of madbyad / mpl-date-time

1. Go to this page and download the library: Download madbyad/mpl-date-time 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/ */

    

madbyad / mpl-date-time example snippets


// This class will store the current date
new Date; 

// This class will store the beggining of the unix timestamp "1 January 1970"
new Date(0); 

// This class will store 1 day after the beggining of the unix timestamp "2 January 1970"
new Date(86400);

// This class will store the current date
new Date(0, true); 

// This class will store the date tommorow
new Date(86400, true); 

// This class will store the date yesterday
new Date(-86400, true); 

$date = new Date(0);

// this will return "1/1/1970"
echo $date->get(DateFormat::D_M_YYYY);

// this will return "01/01/1970"
echo $date->get(DateFormat::DD_MM_YYYY);

// this will return "Thurday 1 January 1970 01:00"
echo $date->get(DateFormat::DAY_MONTH_YYYY__HOUR_MIN);

// this will return "1/1/1970"
echo $date->get(DateFormat::M_D_YYYY);

// this will return "01/01/1970"
echo $date->get(DateFormat::MM_DD_YYYY);

// this will return "January Thurday 1 1970 01:00"
echo $date->get(DateFormat::MONTH_DAY_YYYY__HOUR_MIN);

// this will return "1970/1/1"
echo $date->get(DateFormat::YYYY_M_D);

// this will return "1970/01/01"
echo $date->get(DateFormat::YYYY_MM_DD);

// this will return "1970 January Thurday 1 01:00"
echo $date->get(DateFormat::YYYY_MONTH_DAY__HOUR_MIN);

"1/1/1970"

"01/01/1970"

"Thurday 1 January 1970 01:00"

"1/1/1970"

"01/01/1970"

"January Thurday 1 1970 01:00"

"1970/1/1"

"1970/01/01"

"1970 January Thurday 1 01:00"

$date = new Date(0);

var_dump($date->getArray());

[
    ["day"] => "1",
    ["dayWithZero"] => "01",
    ["dayName"] => "Thursday",
    ["month"] => "1",
    ["monthWithZero"] => "01",
    ["monthName"] => "January",
    ["year"] => "1970",
    ["yearTwoDigit"] => "70",
    ["hour"] => "01",
    ["minute"] => "00",
    ["second"] => "00",
    ["unix"] => "0",
];

// Create a new class that store 0 time (0 second)
new Time();

// Create a new class that store 1 day
new Time(86400);

// Create a new class that store 1 month (30 days)
new Time(86400 * 30);

$time = new Time();

// set the time to be 86400 second of unix timestamp (1 day)
$time->setUnix(86400);

// set the time to be 1 year
$time->setYear(1);

// set the time to be 1 month
$time->setMonth(1);

// set the time to be 1 day
$time->setDay(1);

// set the time to be 1 hour
$time->setHour(1);

// set the time to be 1 minute
$time->setMinute(1);

// set the time to be 1 second
$time->setSecond(1);

$time = new Time();

// add 86400 second of unix timestamp (1 day)
$time->addUnix(86400);

// add 1 year
$time->addYear(1);

// add 1 month
$time->addMonth(1);

// add 1 day
$time->addDay(1);

// add 1 hour
$time->addHour(1);

// add 1 minute
$time->addMinute(1);

// add a second
$time->addSecond(1);

$time = new Time();

// subtract 86400 second of unix timestamp (1 day)
$time->subtractUnix(86400);

// subtract 1 year
$time->subtractYear(1);

// subtract 1 month
$time->subtractMonth(1);

// subtract 1 day
$time->subtractDay(1);

// subtract 1 hour
$time->subtractHour(1);

// subtract 1 minute
$time->subtractMinute(1);

// subtract a second
$time->subtractSecond(1);

// get the unix timestamp
$time->getUnix(86400);

// get the time year
$time->getYear(1);

// get the time month
$time->getMonth(1);

// get the time day
$time->getDay(1);

// get the time hour
$time->getHour(1);

// get the time minute
$time->getMinute(1);

// get the time second
$time->getSecond(1);

$time = new Time();

$time->addUnix(86400);
$time->addYear(1);
$time->addMonth(1);
$time->addDay(1);
$time->addHour(1);
$time->addMinute(1);
$time->addSecond(1);

var_dump($time->get());

[
    ["unix"] => 33872461,
    ["year"] => 1,
    ["month"] => 1,
    ["day"] => 2,
    ["hour"] => 1,
    ["minute"] => 1,
    ["second"] => 1,
];

$time = new Time();

$time->addUnix(86400);
$time->addYear(1);
$time->addMonth(1);
$time->addDay(1);
$time->addHour(1);
$time->addMinute(1);
$time->addSecond(1);

echo $time->getString();

"1 year 1 month 2 days 1 hour 1 minute 1 second"

$date = new Date(0);

$time = new Time();
$time->setDay(15);

$newDate = DateCalculator::add($date, $time);

echo $date->get(DateFormat::DD_MONTH_YYYY);

echo $newDate->get(DateFormat::DD_MONTH_YYYY);

"01 January 1970"
"16 January 1970"

$date = new Date(86400 * 15);

$time = new Time();
$time->setDay(5);

$newDate = DateCalculator::subtract($date, $time);

echo $date->get(DateFormat::DD_MONTH_YYYY);

echo $newDate->get(DateFormat::DD_MONTH_YYYY);

"16 January 1970"
"11 January 1970"

$date = new Date(0);
$secondDate = new Date(86400 * 15);

$timeGap = DateCalculator::timeGap($date, $secondDate);
echo $timeGap->getString();

// also works in reverse
$timeGap = DateCalculator::timeGap($secondDate, $date);
echo $timeGap->getString();

"15 days"
"15 days"