Skip to content

Commit

Permalink
add Datetime
Browse files Browse the repository at this point in the history
  • Loading branch information
AxiosLeo committed Jul 17, 2020
1 parent cef4b29 commit 0818499
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 9 deletions.
55 changes: 55 additions & 0 deletions src/Datetime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace axios\tools;

class Datetime
{
private $base_timestamp;

public function __construct($base_timestamp = null)
{
if (null === $base_timestamp) {
$base_timestamp = time();
}
$this->base_timestamp = $base_timestamp;
}

public function hourBeginEnd($hour)
{
$date = date('Y-m-d', $this->base_timestamp);
$hour = sprintf('%02d', $hour);
$begin = strtotime($date . ' ' . $hour . ':00:00');
$end = strtotime($date . ' ' . $hour . ':00:00 +1 hour -1 seconds');

return [$begin, $end];
}

public function dayBeginEnd($date = null)
{
if (null === $date) {
$date = date('Y-m-d', $this->base_timestamp);
}
$begin = strtotime($date . ' 00:00:00');
$end = strtotime("{$date} +1 day -1 seconds");

return [$begin, $end];
}

public function monthBeginEnd($year = null, $month = null)
{
if (null === $year) {
$year = date('Y', $this->base_timestamp);
}
if (null === $month) {
$month = date('m', $this->base_timestamp);
}
$month = sprintf('%02d', $month);
$ymd = $year . '-' . $month . '-01';
$begin = strtotime($ymd . ' 00:00:00');
$end = strtotime("{$ymd} +1 month -1 seconds");

return [$begin, $end];
}
}
41 changes: 32 additions & 9 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@

class Helper
{
/**
* @param $date
* @param $hour
* @param string $format
*
* @return array
*
* @deprecated use Datetime instead, pls
*/
public static function getHourBeginEndTime($date, $hour, $format = 'timestamp')
{
$hour = sprintf('%02d', $hour);
$begin = strtotime($date . ' ' . $hour . ':00:00');
$end = strtotime($date . ' ' . $hour . ':00:00 +1 hour -1 seconds');
$datetime = new Datetime(strtotime($date));
list($begin, $end) = $datetime->hourBeginEnd($hour);
if ('timestamp' == $format) {
return [
'begin' => $begin,
Expand All @@ -24,10 +32,18 @@ public static function getHourBeginEndTime($date, $hour, $format = 'timestamp')
];
}

/**
* @param $date
* @param string $format
*
* @return array
*
* @deprecated use Datetime instead, pls
*/
public static function getDayBeginEndTime($date, $format = 'timestamp')
{
$begin = strtotime($date . ' 00:00:00');
$end = strtotime("{$date} +1 day -1 seconds");
$datetime = new Datetime();
list($begin, $end) = $datetime->dayBeginEnd($date);
if ('timestamp' == $format) {
return [
'begin' => $begin,
Expand All @@ -41,12 +57,19 @@ public static function getDayBeginEndTime($date, $format = 'timestamp')
];
}

/**
* @param $year
* @param $month
* @param string $format
*
* @return array
*
* @deprecated use Datetime instead, pls
*/
public static function getMonthBeginEndDay($year, $month, $format = 'timestamp')
{
$month = sprintf('%02d', $month);
$ymd = $year . '-' . $month . '-01';
$begin = strtotime($ymd . ' 00:00:00');
$end = strtotime("{$ymd} +1 month -1 seconds");
$datetime = new Datetime();
list($begin, $end) = $datetime->monthBeginEnd($year, $month);
if ('timestamp' == $format) {
return [
'begin' => $begin,
Expand Down

0 comments on commit 0818499

Please sign in to comment.