Last active
November 16, 2021 13:56
-
-
Save szabacsik/b84340b9bab737ff0044a508e32b2d8b to your computer and use it in GitHub Desktop.
Date & Time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
ini_set('display_errors', '1'); | |
ini_set('display_startup_errors', '1'); | |
error_reporting(E_ALL); | |
class time | |
{ | |
public function now ( $format = "Y-m-d H:i:s.u" ) | |
{ | |
list($microSecond, $second) = explode(" ", microtime()); | |
$microSecond = substr(str_replace('0.', '', $microSecond), 0, 6); | |
$date = new \DateTime (date('Y-m-d H:i:s.' . $microSecond, (int)$second)); | |
return $date -> format ( $format ); | |
} | |
public function micro ( $get_as_float = true ) | |
{ | |
return microtime ( $get_as_float ); | |
} | |
} | |
/** | |
* @param \DateTime $from | |
* @param \DateTime $to | |
* @return array | |
* @link https://stackoverflow.com/questions/4312439/php-return-all-dates-between-two-dates-in-an-array#52355368 | |
*/ | |
function getDaysBetweenTwoDates(\DateTime $from, \DateTime $to, $format = null): array | |
{ | |
$result = []; | |
$to = $to->modify('+1 day'); | |
$interval = new \DateInterval('P1D'); | |
$period = new \DatePeriod($from, $interval, $to); | |
foreach ($period as $date) { | |
if (is_null($format)) { | |
$result[] = $date; | |
} else { | |
$result[] = $date->format("Y-m-d"); | |
} | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment