2017-05-21 17:16:32 -04:00
|
|
|
<?php
|
2017-11-16 20:23:18 -05:00
|
|
|
/** @license MIT
|
|
|
|
* Copyright 2017 J. King, Dustin Wilson et al.
|
|
|
|
* See LICENSE and AUTHORS files for details */
|
|
|
|
|
2017-05-21 17:16:32 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
namespace JKingWeb\Arsse\Misc;
|
|
|
|
|
2020-12-08 15:34:31 -05:00
|
|
|
abstract class Date {
|
2018-01-02 16:53:38 -05:00
|
|
|
public static function transform($date, string $outFormat = null, string $inFormat = null) {
|
|
|
|
$date = ValueInfo::normalize($date, ValueInfo::T_DATE, $inFormat);
|
|
|
|
if (!$date) {
|
|
|
|
return null;
|
2017-07-20 22:40:09 -04:00
|
|
|
}
|
2018-01-02 16:53:38 -05:00
|
|
|
$out = ValueInfo::normalize($date, ValueInfo::T_STRING, null, $outFormat);
|
2019-01-11 10:38:06 -05:00
|
|
|
if ($outFormat === "unix") {
|
2018-01-02 16:53:38 -05:00
|
|
|
$out = (int) $out;
|
2019-01-11 10:38:06 -05:00
|
|
|
} elseif ($outFormat === "float") {
|
2018-01-02 16:53:38 -05:00
|
|
|
$out = (float) $out;
|
2017-07-20 22:40:09 -04:00
|
|
|
}
|
2018-01-02 16:53:38 -05:00
|
|
|
return $out;
|
2017-05-21 17:16:32 -04:00
|
|
|
}
|
|
|
|
|
2020-01-20 13:52:48 -05:00
|
|
|
public static function normalize($date, string $inFormat = null): ?\DateTimeImmutable {
|
2017-10-19 22:39:39 -04:00
|
|
|
return ValueInfo::normalize($date, ValueInfo::T_DATE, $inFormat);
|
2017-05-21 17:16:32 -04:00
|
|
|
}
|
2017-08-17 22:36:15 -04:00
|
|
|
|
2020-01-20 13:52:48 -05:00
|
|
|
public static function add($interval, $date = "now"): ?\DateTimeImmutable {
|
2017-08-17 22:36:15 -04:00
|
|
|
return self::modify("add", $interval, $date);
|
|
|
|
}
|
|
|
|
|
2020-01-20 13:52:48 -05:00
|
|
|
public static function sub($interval, $date = "now"): ?\DateTimeImmutable {
|
2017-08-17 22:36:15 -04:00
|
|
|
return self::modify("sub", $interval, $date);
|
|
|
|
}
|
|
|
|
|
2020-01-20 13:52:48 -05:00
|
|
|
protected static function modify(string $func, $interval, $date): ?\DateTimeImmutable {
|
2018-01-02 16:53:38 -05:00
|
|
|
$date = self::normalize($date);
|
2019-01-20 22:40:49 -05:00
|
|
|
$interval = (!$interval instanceof \DateInterval) ? ValueInfo::normalize($interval, ValueInfo::T_INTERVAL) : $interval;
|
|
|
|
return $date ? $date->$func($interval) : null;
|
2017-08-17 22:36:15 -04:00
|
|
|
}
|
2017-08-29 10:50:31 -04:00
|
|
|
}
|