PHP code example of prokki / ext-datetime

1. Go to this page and download the library: Download prokki/ext-datetime 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/ */

    

prokki / ext-datetime example snippets


use ExtDateTime\DateTime;
use ExtDateTime\DateTimeImmutable;

// create a datetime object
$dateTime = new DateTime("now");

// create an immutable datetime object
$dateTimeImmutable = new DateTimeImmutable("now");

use ExtDateTime\DateTime;

// create a datetime object
// and use chaining immediately
$dateTime = DateTime::create("now")
            ->addHours(5)
            ->addDays(5)
            ;

use ExtDateTime\DateTime;

$currentMicroseconds = DateTime::current()->format("u");         // output example 654321
$noMicroseconds      = DateTime::create("now")->format("u");     // output always 000000

use ExtDateTime\DateTimeImmutable;

// create an immutable datetime object from a native non-immutable object
$datetime = DateTimeImmutable::createFromObject(new \DateTime());

use ExtDateTime\DateTime;

// create a datetime object
$datetime = DateTime::current();

// clone the datetime object and proceed like usual with chaining
$clone = $datetime->duplicate()
         ->addHours(5)
         ->addDays(5)
         ;

use ExtDateTime\DateTime;

// create a datetime object
$datetime = DateTime::current();

$clonedImmutable = $datetime->toImmutable();       // only available in class DateTime

$clonedMutable   = $clonedImmutable->toMutable();  // only available in class DateTimeImmutable

use ExtDateTime\DateTime;

// create a datetime object and add 10 hours
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->addHours(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 22:35:17"

// create a datetime object and subtracts 10 hours
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->subHours(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 02:35:17"

use ExtDateTime\DateTime;

// create a datetime object and add 10 days
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->addDays(10)
                  ->format("Y-m-d h:i:s");                   // "2020-08-09 22:35:17"

// create a datetime object and subtracts 10 days
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->subDays(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-20 02:35:17"

use ExtDateTime\DateTime;

// create a datetime object and add 10 months
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->addMonth(10)
                  ->format("Y-m-d h:i:s");                   // "2020-08-09 22:35:17"

// create a datetime object and subtracts 10 months
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->subMonth(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-20 02:35:17"

use ExtDateTime\DateTime;

// create a datetime object and sets the time to the end of the day
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toEndOfDay(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 23:59:59"

use ExtDateTime\DateTime;

// create a datetime object and sets the time to noon
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toNoon(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 12:00:00"

use ExtDateTime\DateTime;

// create a datetime object and sets the time the start of the day
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toStartOfDay(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 00:00:00"

use ExtDateTime\DateTime;

// create a datetime object and sets the date to the first day of the month
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toStartOfMonth()
                  ->format("Y-m-d h:i:s");                   // "2020-07-01 00:00:00"

use ExtDateTime\DateTime;

// create a datetime object and sets the date to the last day of the month
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toEndOfMonth()
                  ->format("Y-m-d h:i:s");                   // "2020-07-31 23:59:59"