-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMWTimestampHelper.php
More file actions
29 lines (22 loc) · 939 Bytes
/
MWTimestampHelper.php
File metadata and controls
29 lines (22 loc) · 939 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/* The purpose of this class is to help convert between Carbon objects
* used in this Platform API application and MWTimestamps which are used
* internally in some Mediawiki databases.
* See: https://www.mediawiki.org/wiki/Manual:Timestamp
*/
namespace App\Helper;
use Carbon\CarbonImmutable;
use Carbon\Exceptions\InvalidFormatException;
class MWTimestampHelper {
private const string MWTimestampFormat = 'YmdHis';
public static function getCarbonFromMWTimestamp(string $MWTimestamp): CarbonImmutable {
$carbon = CarbonImmutable::createFromFormat(self::MWTimestampFormat, $MWTimestamp);
if ($carbon === null) {
throw new InvalidFormatException('Unable to create Carbon object');
}
return $carbon;
}
public static function getMWTimestampFromCarbon(CarbonImmutable $carbonImmutable): string {
return $carbonImmutable->format(self::MWTimestampFormat);
}
}