Extends PHP's DateTime class by SAP week, date, time and timestamp format. Extends PHP's DateInterval class by SAP time format.
- SAP week in format
<year><week>
- SAP date in format
<year><month><day>
- SAP time in format
<hour><minute><second>
- SAP timestamp in format
<year><month><day><hour><minute><second>
composer require php-sap/datetime:~1.0.0
<?php
use phpsap\DateTime\SapDateTime;
$dateTime = SapDateTime::createFromFormat(SapDateTime::SAP_WEEK, '201846');
echo $dateTime->format('o \w\e\ek W') . PHP_EOL;
/**
* Output: 2018 week 46
*/
<?php
use phpsap\DateTime\SapDateTime;
$dateTime = new SapDateTime('2018-10-19 08:09:10');
echo $dateTime->format(SapDateTime::SAP_WEEK) . PHP_EOL;
/**
* Output: 201842
*/
<?php
use phpsap\DateTime\SapDateTime;
$dateTime = SapDateTime::createFromFormat(SapDateTime::SAP_DATE, '20181101');
echo $dateTime->format('Y-m-d') . PHP_EOL;
/**
* Output: 2018-11-01
*/
<?php
use phpsap\DateTime\SapDateTime;
$dateTime = new SapDateTime('2018-12-31 09:10:11');
echo $dateTime->format(SapDateTime::SAP_DATE) . PHP_EOL;
/**
* Output: 20181231
*/
<?php
use phpsap\DateTime\SapDateTime;
$dateTime = SapDateTime::createFromFormat(SapDateTime::SAP_TIME, '132001');
echo $dateTime->format('H:i:s') . PHP_EOL;
/**
* Output: 13-20-01
*/
This example uses 25 hours to demonstrate the usage of a DateInterval object
in combination with a DateTime object. Please beware, that the format()
method is more like PHP's sprintf()
and less like date()
.
<?php
use phpsap\DateTime\SapDateInterval;
$time = SapDateInterval::createFromDateString('252003');
echo $time->format('%H:%I:%S') . PHP_EOL;
/**
* Output: 25:20:03
*/
$date = new DateTime('2018-11-01 00:00:00');
$date->add($time);
echo $date->format('Y-m-d H:i:s');
/**
* Output: 2018-11-02 01:20:03
*/
<?php
use phpsap\DateTime\SapDateTime;
$dateTime = new SapDateTime('21:45:05');
echo $dateTime->format(SapDateTime::SAP_TIME) . PHP_EOL;
/**
* Output: 214505
*/
<?php
use phpsap\DateTime\SapDateInterval;
$time = new SapDateInterval('PTH21M45S05');
echo $time->format(SapDateInterval::SAP_TIME) . PHP_EOL;
/**
* Output: 214505
*/
<?php
use phpsap\DateTime\SapDateTime;
$dateTime = SapDateTime::createFromFormat(SapDateTime::SAP_TIMESTAMP, '20181019080910');
echo $dateTime->format('Y-m-d H:i:s') . PHP_EOL;
/**
* Output: 2018-10-19 08:09:10
*/
<?php
use phpsap\DateTime\SapDateTime;
$dateTime = new SapDateTime('2018-12-31 09:10:11');
echo $dateTime->format(SapDateTime::SAP_TIMESTAMP) . PHP_EOL;
/**
* Output: 20181231091011
*/