PHP code example of psx / datetime

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

    

psx / datetime example snippets




use PSX\DateTime\Duration;
use PSX\DateTime\LocalDate;
use PSX\DateTime\LocalDateTime;
use PSX\DateTime\LocalTime;
use PSX\DateTime\Period;

// date time
$dateTime = LocalDateTime::parse('2023-03-22T22:56:00Z');
$dateTime = LocalDateTime::of(2023, 3, 22, 22, 56, 0);

$dateTime->getYear(); // 2023
$dateTime->getMonth(); // Month::MARCH
$dateTime->getMonthValue(); // 3
$dateTime->getDayOfMonth(); // 22
$dateTime->getDayOfWeek(); // 3
$dateTime->getHour(); // 22
$dateTime->getMinute(); // 56
$dateTime->getSecond(); // 0

$dateTime->plusDays(1);
$dateTime->minusDays(1);
$dateTime->withDayOfMonth(1);

echo $dateTime->toString(); // 2016-03-28T23:27:00Z

// date
$date = LocalDate::parse('2023-03-22');
$date = LocalDate::of(2023, 3, 22);

echo $date->toString(); // 2023-03-22

// time
$time = LocalTime::parse('23:27:00');
$time = LocalTime::of(23, 27, 0);

echo $time->toString(); // 23:27:00

// period
$period = Period::parse('P1D');
$period = Period::of(1, 0, 0);

echo $period->toString(); // P1D

// duration
$duration = Duration::parse('P1H');
$duration = Duration::of(1, 0, 0);

echo $duration->toString(); // PT1H