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