Php 如何在CL中构造可重用的DateTime对象?
我一直在尝试在CodeIgniter中构造一个Php 如何在CL中构造可重用的DateTime对象?,php,codeigniter,datetime,Php,Codeigniter,Datetime,我一直在尝试在CodeIgniter中构造一个DateTime对象。我正在从图书馆的下载。但我似乎无法用给定的$timeZone参数构造DateTime() 我看到这种情况的唯一方法是在所有方法中添加DateTime对象 这是使用mvc的一大缺点。有人有别的选择吗 我的代码: class Create_Timestamp { private $date_time; /** * @param $time_zone : gets passed in from a c
DateTime对象。我正在从图书馆的下载。但我似乎无法用给定的$timeZone参数构造DateTime()
我看到这种情况的唯一方法是在所有方法中添加DateTime对象
这是使用mvc的一大缺点。有人有别的选择吗
我的代码:
class Create_Timestamp
{
private $date_time;
/**
* @param $time_zone : gets passed in from a cookie. Cookie value is an array that gets retrieved from the DB. In the
* array there time zone value
* @return int
*/
public function __construct($time_zone)
{
$this->date_time = new DateTime("now", new DateTimeZone($time_zone));
}
public function TimeStamp()
{
$time_stamp = $this->date_time->getTimestamp();
return $time_stamp;
}
public function convertTimeStamp($unix_time_stamp)
{
fb($unix_time_stamp);
$this->date_time->createFromFormat("m/d/Y", $unix_time_stamp);
//$this->date_time->setTimestamp($unix_time_stamp);
$formatted_time_stamp = $this->date_time->format("m/d/Y h:i:s A");
return $formatted_time_stamp;
}
}
不要重新发明轮子做馅饼(自豪地在别处发明)。你会为自己和他人省去很多悲伤
以包为例
如果你有幸使用CI v.3,只需转到你的应用程序文件夹并执行以下操作
$ composer require nesbot/carbon
然后开始使用它
Carbon\Carbon::createFromTimestamp($time\u stamp,$time\u zone)->toDateTimeString();
如果您使用的是CI v.2.X,仍然利用composer。转到你的应用程序根目录并执行以下操作
$ composer require nesbot/carbon
然后在index.php中添加
require./vendor/autoload.php';//或者不管你的处境如何
请记住格式化您的代码。