如何在codeigniter中将时间戳转换为日期 [英] how to convert timestamp to date in codeigniter
问题描述
我想在Codeigniter中将 但是,通过使用我编写的函数转换时间戳,我仍然收到完全不同的结果,请注意:我需要根据不同的时区更改日期,这就是为什么我要这样写: 1373892900000
转换为 Monday 2013/07/15 8:55 。 p>
公共函数time_convert($ timestamp){
$ this-> load-> helper('date');
date_default_timezone_set('UTC');
$ daylight_saving = TRUE;
$ timezone = UM4; //多伦多或纽约时区
$ time = gmt_to_local($ timestamp,$ timezone,$ daylight_saving);
$ final_time = standard_date('DATE_RFC822',$ time);
返回$ final_time;
}
上述功能的结果为: 06年12月8日星期六01:40:00 +0000
如果我不输入 date_default_timezone_set( 'UTC');
在上述函数中,我得到的日期是 Sat,08 Dec 06 02:40:00 +0100
。我的codeigniter似乎将时区默认为欧洲/柏林。
有人可以帮助我纠正我可能犯的任何错误吗?
为什么不只使用PHP的日期函数?
public函数time_convert($ timestamp){
返回日期('l Y / m / d H:i',$ timestamp);
}
对于不同的时区,请使用DateTime对象:
公共函数time_convert($ timestamp,$ timezone ='UTC'){
$ datetime = new DateTime($ timestamp,new DateTimeZone($ timezone) );
返回$ datetime-> format(’l Y / m / d H:i’);
}
这应该可行。注意:我认为您至少需要TimeZone类的PHP版本5.20。
I want to convert 1373892900000
to Monday 2013/07/15 8:55 AM
in Codeigniter.
However, I keep receiving a totally different result by converting the timestamp using the function i have written, please note:I need to change the dates according to different timezones, that is why I want to write it this way:
public function time_convert($timestamp){
$this->load->helper('date');
date_default_timezone_set('UTC');
$daylight_saving = TRUE;
$timezone = "UM4"; //toronto or new york timezone
$time = gmt_to_local($timestamp, $timezone, $daylight_saving);
$final_time = standard_date('DATE_RFC822', $time);
return $final_time;
}
Result from the above function is: Sat, 08 Dec 06 01:40:00 +0000
And if I don't put date_default_timezone_set('UTC');
in the above function, I get this date instead Sat, 08 Dec 06 02:40:00 +0100
. My codeigniter seems to default the timezone to Europe/Berlin.
Can anyone please help me correct any of the mistakes I might have made?
Why not just use PHP's date function?
public function time_convert($timestamp){
return date('l Y/m/d H:i', $timestamp);
}
For different timezones use a DateTime object:
public function time_convert($timestamp, $timezone = 'UTC'){
$datetime = new DateTime($timestamp, new DateTimeZone($timezone));
return $datetime->format('l Y/m/d H:i');
}
Think that should work. Note: I tihnk you need at least PHP version 5.20 for the TimeZone class.
这篇关于如何在codeigniter中将时间戳转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!