我已经搜索此问题2天了,但是没有运气。我想向laravel carbon添加自定义方法。我使用以下语法:
namespace App\Http\Controllers\Helpers;
use Carbon\CarbonInterface;
interface CarbonJalaliInterface extends CarbonInterface {
public function toJalaliDateTimeString($unitPrecision = 'second');
public function toJalaliDateString($unitPrecision = 'second');
}
abstract class CarbonJalali implements CarbonJalaliInterface {
public function toJalaliDateTimeString($unitPrecision = 'second') {
list($part1, $part2) = explode(' ', $this->rawFormat('Y-m-d '.static::getTimeFormatByPrecision($unitPrecision)));
list($year, $month, $day) = explode('-', $part1);
list($hour, $minute, $second) = explode(':', $part2);
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
include_once ('jdf.php');
$jalali_date = jdate("H:i:s - Y/m/d",$timestamp);
return $jalali_date;
}
public function toJalaliDateString($unitPrecision = 'second') {
$timezone = 0;
list($part1, $part2) = explode(' ', $this->rawFormat('Y-m-d'));
list($year, $month, $day) = explode('-', $part1);
$timestamp = mktime($month, $day, $year);
include_once ('jdf.php');
$jalali_date = jdate("Y/m/d",$timestamp);
return $jalali_date;
}
}
但是我得到了错误:
Symfony\Component\Debug\Exception\FatalErrorException
DateTimeInterface can't be implemented by user classes
我想这样使用它:
CarbonJalali::now()->toJalaliDateTimeString();
有什么建议吗?谢谢。
已解决
由于@lagbox,因此无需使用实现,其工作语法如下:
namespace App\Http\Controllers\Helpers;
class CarbonJalali extends Carbon {
public function toJalaliDateTimeString($unitPrecision = 'second') {
list($part1, $part2) = explode(' ', $this->rawFormat('Y-m-d '.static::getTimeFormatByPrecision($unitPrecision)));
list($year, $month, $day) = explode('-', $part1);
list($hour, $minute, $second) = explode(':', $part2);
$timestamp = mktime($hour, $minute, $second, $month, $day, $year);
include_once ('jdf.php');
$jalali_date = jdate("H:i:s - Y/m/d",$timestamp);
return $jalali_date;
}
public function toJalaliDateString($unitPrecision = 'second') {
$timezone = 0;
list($part1, $part2) = explode(' ', $this->rawFormat('Y-m-d'));
list($year, $month, $day) = explode('-', $part1);
$timestamp = mktime($month, $day, $year);
include_once ('jdf.php');
$jalali_date = jdate("Y/m/d",$timestamp);
return $jalali_date;
}
}