doudu161481 2010-09-24 10:41 采纳率: 100%
浏览 96
已采纳

在PHP中创建新的DateTime对象不起作用

I have the following piece of code. I've recorded the output as well:

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(
//))

SOmething is wrong with my php's DateTime() funciton - but I cannot fathom what! I'm using PHP 5.2.14 on this server.


Edit 1: Sorry, misinterpreted some PHP output - corrected it above


Edit 2: I had the following test file which gave the exactly output as below

<?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);

?>

Output:

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) {
}

Edit 3 - if it helps, my phpinfo shows this as well

date
date/time support   enabled
"Olson" Timezone Database Version   2010.12
Timezone Database   external
Default timezone    America/Chicago 
  • 写回答

3条回答 默认 最新

  • doumao1917 2010-09-24 10:47
    关注

    I simplified and ran your code. This is the output:

    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"
    }
    

    I don't see a problem.

    Your echo at the top contains junk at the end (after the timezone)

    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
    

    What's all the extra stuff at the end?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 uniapp连接阿里云无法发布消息和订阅
  • ¥25 麦当劳点餐系统代码纠错
  • ¥15 轮班监督委员会问题。
  • ¥15 基于作物生长模型下,有限水资源的最大化粮食产量的资源优化模型建立
  • ¥20 关于变压器的具体案例分析
  • ¥15 生成的QRCode圖片加上下載按鈕
  • ¥15 板材切割优化算法,数学建模,python,lingo
  • ¥15 科来模拟ARP欺骗困惑求解
  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver