如何在codeigniter中将时间戳转换为日期

编程入门 行业动态 更新时间:2024-10-24 01:48:06
本文介绍了如何在codeigniter中将时间戳转换为日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在Codeigniter中将 1373892900000 转换为 Monday 2013/07/15 8:55

但是,通过使用我编写的函数转换时间戳,我仍然收到完全不同的结果,请注意:我需要根据不同的时区更改日期,这就是为什么我要这样写:

公共函数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中将时间戳转换为日期

本文发布于:2023-10-27 23:30:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534851.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   中将   日期   时间   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!