php - 时间戳或根据时区更改时间
问题描述
我是这个论坛的新手,正在学习编码。
我想问一下如何将时间戳更改为当前时区,或者有什么方法可以将时间戳日期/时间转换为我当前的时区美国/多伦多或 -4:00
我尝试了所有可用于 MySQL 和 Cpanel 的选项。(htacces, phpini, mysql 中的 settimezone)
现在我打算用 PHP 做,因为它会更好。
这是从表中获取时间戳的代码
<?php echo htmlentities($result->PostingDate);?>
请告诉我将时间转换为当前时区的合适方法
解决方案
如果您知道您的 PostingDate 设置的当前时区,并且您想使用 PHP 代码执行此操作,您可以尝试以下操作:
// Current timezone used by server, database
$current_timezone = new DateTimeZone('UTC');
// Your timezone that YOU want to use
$local_timezone = new DateTimeZone('America/Toronto');
$datetime = new DateTime($result->PostingDate, $current_timezone);
$datetime->setTimezone($local_timezone);
// $datetime should be set to America/Toronto now.
推荐阅读
- autodesk-forge - 数据管理 API 403 错误:无法验证此用户
- azure - API-Key 上的 ADF 替换函数问题
- nservicebus - 调用/请求创建的 IHandleMessages 实例
- c# - WPF 的第一步:如何填充网格中的单元格?
- java - 如何解压缩 jsonlz4 Mozilla Firefox 会话文件?
- .net - 在没有 Visual Studio 的情况下安装 Xamarin
- java - Apache Tomcat 服务器子域
- c++ - 如何在 c++ win32 中为鼠标操作添加事件挂钩?
- c++ - gtkmm4 应用程序与 MacOS 菜单栏的集成
- javascript - 用于在粘性导航中显示活动航向的交叉点观察器