PHP 日期创建格式在函数/非对象错误中



我有以下代码产生此错误: 致命错误:在第 58 行的 C:\wamp\www\grid2.php 中的非对象上调用成员函数 setTimeZone()

我正在尝试设置此函数以接收如下所示的日期字符串,应用时间偏移量,然后将其以函数调用中设置的格式输出回...但没有运气。

<?php
if (!session_id()) session_start();
$_SESSION["timeoffset"] = "Europe/Amsterdam";

function formatDate($date, $format){
// use the the appropriate timezone for your stamp
$timestamp = DateTime::createFromFormat($format, $date, new DateTimeZone('UTC'));
// set it to whatever you want to convert it
$timestamp->setTimeZone(new DateTimeZone($_SESSION["timeoffset"]));
echo $timestamp->format($format);   
}
formatDate('2012-10-14T21:15', 'Y-m-dTH:i');
?>

我还试图弄清楚如何返回具有一定分钟数的字符串。

所以字符串"2012-10-14T21:15"将增加 5 分钟 = "2012-10-14T21:20"

任何帮助将不胜感激!

杰夫

DateTime::createFromFormat 将在失败时返回 false。这意味着:

DateTime::createFromFormat($format, $date, new DateTimeZone('UTC'));

失败了,$timestamp是假的(var_dump($timestamp)将显示给你)。在不知道您作为函数 formatDate 的参数传入的内容的情况下,我认为没有人能够确切地弄清楚您做错了什么。

使用这个

$timestamp->setTimeZone(new DateTimeZone("Europe/Amsterdam"));

我认为sessions数组没有返回字符串Europe/Amsterdam.

执行调试

最新更新