| 1 |
<?php |
| 2 |
|
| 3 |
namespace Rakit\Validation\Rules\Traits; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
|
| 7 |
trait DateUtilsTrait |
| 8 |
{ |
| 9 |
|
| 10 |
/** |
| 11 |
* Check the $date is valid |
| 12 |
* |
| 13 |
* @param string $date |
| 14 |
* @return bool |
| 15 |
*/ |
| 16 |
protected function isValidDate(string $date): bool |
| 17 |
{ |
| 18 |
return (strtotime($date) !== false); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Throw exception |
| 23 |
* |
| 24 |
* @param string $value |
| 25 |
* @return Exception |
| 26 |
*/ |
| 27 |
protected function throwException(string $value) |
| 28 |
{ |
| 29 |
// phpcs:ignore |
| 30 |
return new Exception("Expected a valid date, got '{$value}' instead. 2016-12-08, 2016-12-02 14:58, tomorrow are considered valid dates"); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Given $date and get the time stamp |
| 35 |
* |
| 36 |
* @param mixed $date |
| 37 |
* @return int |
| 38 |
*/ |
| 39 |
protected function getTimeStamp($date): int |
| 40 |
{ |
| 41 |
return strtotime($date); |
| 42 |
} |
| 43 |
} |
| 44 |
|