我有以下代码。我也记录了输出:
function convertGeneralAvailabilityTime($date,$from_timezone,$from_timebegin, $from_time$
{
echo "$date,$from_timezone,$from_timebegin, $from_timeend, $to_timezone";
// 2010-09-19,America/New_York,07:45:00, 08:00:00, America/Los_Angeles
$tz1 = new DateTimezone($from_timezone);
$datetime1 = new DateTime("$date $from_timebegin", $tz1);
$datetime2 = new DateTime("$date $from_timeend", $tz1);
echo "$date $from_timebegin";
// 2010-09-19 07:45:00
echo "$date $from_timeend";
// 2010-09-19 08:00:00
var_export($tz1);
//DateTimeZone::__set_state(array(
//))
var_export($datetime1);
//DateTime::__set_state(array(
//))
我的php的DateTime()函数出了问题 - 但我无法理解!我在这台服务器上使用PHP 5.2.14。
编辑1:抱歉,误解了一些PHP输出 - 在上面更正了
编辑2:我有以下测试文件,它给出了如下的确切输出
<?php
$date = '2010-09-19';
$from_timezone = 'America/New_York';
$from_timebegin = '07:45:00';
$from_timeend = '08:00:00';
$to_timezone = 'America/Los_Angeles'; // Trimmed 2010-09-19 07:45:002010-09-19
$tz1 = new DateTimezone($from_timezone);
$datetime1 = new DateTime("$date $from_timebegin", $tz1);
$datetime2 = new DateTime("$date $from_timeend", $tz1);
echo "$date $from_timebegin".PHP_EOL;
echo "$date $from_timeend".PHP_EOL;
var_dump($tz1);
var_dump($datetime1);
?>
输出:
jailshell-3.2$ php dttest.php
2010-09-19 07:45:00
2010-09-19 08:00:00
object(DateTimeZone)#1 (0) {
}
object(DateTime)#2 (0) {
}
编辑3 - 如果有帮助,我的phpinfo也会显示
date
date/time support enabled
"Olson" Timezone Database Version 2010.12
Timezone Database external
Default timezone America/Chicago
答案 0 :(得分:1)
在创建DateTime对象之前声明您的默认时区,例如。
date_default_timezone_set('America/New_York');
$tz1 = new DateTimezone($from_timezone);
$datetime1 = new DateTime("$date $from_timebegin", $tz1);
$datetime2 = new DateTime("$date $from_timeend", $tz1);
或者在你的php.ini文件中使用date.timezone =“America / New_York”声明它
答案 1 :(得分:0)
我简化并运行了你的代码。这是输出:
php > $date = '2010-09-19';
php > $from_timezone = 'America/New_York';
php > $from_timebegin = '07:45:00';
php > $from_timeend = '08:00:00';
php > $to_timezone = 'America/Los_Angeles'; // Trimmed 2010-09-19 07:45:002010-09-19 08:00:00
php >
php > $tz1 = new DateTimezone($from_timezone);
php >
php > $datetime1 = new DateTime("$date $from_timebegin", $tz1);
php > $datetime2 = new DateTime("$date $from_timeend", $tz1);
php >
php > echo "$date $from_timebegin".PHP_EOL;
2010-09-19 07:45:00
php > echo "$date $from_timeend".PHP_EOL;
2010-09-19 08:00:00
php > var_dump($tz1);
object(DateTimeZone)#1 (0) {
}
php > var_dump($datetime1);
object(DateTime)#2 (3) {
["date"]=>
string(19) "2010-09-19 07:45:00"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "America/New_York"
}
我没有看到问题。
顶部的回声包含末尾的垃圾(时区之后)
echo "$date,
$from_timezone,
$from_timebegin,
$from_timeend,
$to_timezone"
;
// 2010-09-19,
// America/New_York,
// 07:45:00,
// 08:00:00,
// America/Los_Angeles2010-09-19 07:45:002010-09-19 08:00:00
最后还有什么额外的东西?
答案 2 :(得分:-3)
$datetime1 = new DateTime($date $from_timebegin, $tz1);
$datetime2 = new DateTime($date $from_timeend, $tz1);
echo $date $from_timebegin;
echo $date $from_timeend;