Skip to content

Commit

Permalink
Update Date Helper
Browse files Browse the repository at this point in the history
  • Loading branch information
hungnguyenhp committed Sep 22, 2021
1 parent ebaf0e8 commit 64660e5
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/DateAndTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
use Exception;

if (!class_exists('nguyenanhung\Libraries\DateAndTime\DateAndTime')) {
/**
* Class DateAndTime
*
* @package nguyenanhung\Libraries\DateAndTime
* @author 713uk13m <dev@nguyenanhung.com>
* @copyright 713uk13m <dev@nguyenanhung.com>
*/
class DateAndTime
{
/**
Expand Down Expand Up @@ -75,5 +82,72 @@ public static function generateOTPExpireTime($hour = 4)

return $time->format('Y-m-d H:i:s');
}

/**
* Function filterDate
*
* @param string $inputDate
*
* @return array
* @author : 713uk13m <dev@nguyenanhung.com>
* @copyright: 713uk13m <dev@nguyenanhung.com>
* @time : 09/22/2021 25:54
*/
public static function filterDate($inputDate = '')
{
if ($inputDate !== '') {
// Get date
if ($inputDate === 'back_1_day') {
try {
$dateTime = new DateTime("-1 day");
$result = array(
'date' => $dateTime->format('Y-m-d'),
'day' => $dateTime->format('Ymd'),
'month' => $dateTime->format('Y-m'),
'monthTable' => $dateTime->format('Y_m'),
'week' => $dateTime->format('w'),
'months' => $dateTime->format('m'),
'year' => $dateTime->format('Y'),
);
} catch (Exception $e) {
if (function_exists('log_message')) {
$message = 'Error Code: ' . $e->getCode() . ' - File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Message: ' . $e->getMessage();
log_message('error', $message);
}
$result = array(
'date' => date('Y-m-d', strtotime("-1 day", strtotime($inputDate))),
'day' => date('Ymd', strtotime("-1 day", strtotime($inputDate))),
'month' => date('Y-m', strtotime("-1 day", strtotime($inputDate))),
'monthTable' => date('Y_m', strtotime("-1 day", strtotime($inputDate))),
'week' => date('w', strtotime("-1 day", strtotime($inputDate))),
'months' => date('m', strtotime("-1 day", strtotime($inputDate))),
'year' => date('Y', strtotime("-1 day", strtotime($inputDate)))
);
}
} else {
$result = array(
'date' => date('Y-m-d', strtotime($inputDate)),
'day' => date('Ymd', strtotime($inputDate)),
'month' => date('Y-m', strtotime($inputDate)),
'monthTable' => date('Y_m', strtotime($inputDate)),
'week' => date('w', strtotime($inputDate)),
'months' => date('m', strtotime($inputDate)),
'year' => date('Y', strtotime($inputDate)),
);
}
} else {
$result = array(
'date' => date('Y-m-d'),
'day' => date('Ymd'),
'month' => date('Y-m'),
'monthTable' => date('Y_m'),
'week' => date('w'),
'months' => date('m'),
'year' => date('Y'),
);
}

return $result;
}
}
}

0 comments on commit 64660e5

Please sign in to comment.