This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTime.php
65 lines (57 loc) · 1.51 KB
/
Time.php
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace Utilities\Common;
/**
* Time class
*
* @link https://github.com/utilities-php/common
* @author Shahrad Elahi (https://github.com/shahradelahi)
* @license https://github.com/utilities-php/common/blob/master/LICENSE (MIT License)
*/
class Time
{
/**
* Get the current elapsed time from request in milliseconds
*
* @return float
*/
public static function elapsedTime(): float
{
return round(((microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]) * 1000));
}
/**
* Get the current time in milliseconds
*
* @param int|string $time [optional] The timestamp to convert
* @return int
*/
public static function getMillisecond(int|string $time = ''): int
{
$time = $time != '' ? strtotime($time) : microtime(true);
return (int)round($time * 1000);
}
/**
* Get Time in UTC format
*
* @param int $time
* @param bool $zeroTime By setting this to true, it will return 'Y-m-d' format
* @return string
*/
public static function UTCFormat(int $time, bool $zeroTime = false): string
{
date_default_timezone_set('UTC');
if ($zeroTime) {
return date("Y-m-d", $time) . "T00:00:00Z";
}
return date("Y-m-d", $time) . "T" . date("H:i:s", $time) . "Z";
}
/**
* Get instance of DateTime
*
* @return \DateTime
*/
public static function now(): \DateTime
{
return new \DateTime();
}
}