Skip to content

Instantly share code, notes, and snippets.

@banqhsia
Created May 5, 2020 17:00
Show Gist options
  • Save banqhsia/52832b26ee47e937e1ef105c75d8376f to your computer and use it in GitHub Desktop.
Save banqhsia/52832b26ee47e937e1ef105c75d8376f to your computer and use it in GitHub Desktop.
Macros additional useful methods to PHP Carbon package.
<?php
/**
* 給定時間日期是否合法
*
* @param string|null $time
* @param \DateTimeZone|string|null $tz
* @return bool
*/
Carbon::macro('recognized', function ($time = null, $tz = null) {
return (bool) @static::parse($time, $tz);
});
/**
* 給定時間日期是否「不」合法
*
* @param string|null $time
* @param \DateTimeZone|string|null $tz
* @return bool
*/
Carbon::macro('unrecognized', function ($time = null, $tz = null) {
return ! static::recognized(...func_get_args());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment