【问题标题】:PHP: DATETIME helpPHP:日期时间帮助
【发布时间】:2011-05-10 11:47:53
【问题描述】:

我知道互联网上有大量关于日期时间的信息,但我无法使用我所看到的信息。

我有一个连接到文本框的日历控件。它采用 15/11/2010(英国)的形式。我用它来查询 SQL Server 数据库中的日期时间字段,格式为 15/11/2010 00:00:00。

在回发捕获日期后,我将值传递给尝试将文本转换为时间格式的方法。当我查询 2010 年 1 月 1 日到 2010 年 1 月 7 日之间的某些内容时,我得到了许多结果。当我将其更改为 01/01/2010 和 30/10/2010 之间时,我收到一个错误。该错误是无效的 foreach (尚未发现错误)。我猜这与我的格式有关?

function getSupportTicketsbySubIssueAndDate($subissueid, $from, $to){

    $subissueid = $this->ms_escape_string($subissueid);

    $from = date("DD/MM/YY", strtotime($from));
    $to = date("DD/MM/YY", strtotime($to));

    $connection = new Connections();
    $conn = $connection->connectToWarehouse();
    $atid = $_SESSION['saveddata']['autotaskid'];


    $tsql = "SELECT wh_task.task_id, wh_task.task_name, wh_task.task_description, wh_task.task_number, wh_task.reported_by_name, wh_task.create_time, wh_resource.first_name, wh_resource.last_name, wh_task_status.task_status_name ".
            "FROM wh_task_status INNER JOIN (wh_task INNER JOIN wh_resource ON wh_task.assigned_resource_id = wh_resource.resource_id) ON wh_task_status.task_status_id = wh_task.task_status_id ".
            "WHERE (account_id = $atid) AND (subissue_type_id = $subissueid) AND (((wh_task.create_time) Between '$from' And '$to'))".
            "ORDER BY create_time DESC";


    // set up array to hold each of the issue types and the number of tickets in each
    $ticket;

    $stmt = sqlsrv_query( $conn, $tsql);
    if( $stmt === false)
    {
             echo "Error in query preparation/execution.\n";
             die( print_r( sqlsrv_errors(), true));
    }

    $x = 0;
    /* Retrieve each row as an associative array and display the results.*/
    while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
    {
            $ticket[$x][0] = $row['task_name'];
            $ticket[$x][1] = $row['task_description'];
            $ticket[$x][2] = $row['task_number'];
            $ticket[$x][3] = $row['reported_by_name'];
            $ticket[$x][4] = $row['first_name'];
            $ticket[$x][5] = $row['last_name'];
            $ticket[$x][6] = $row['task_status_name'];
            $ticket[$x][7] = $row['create_time'];
            $ticket[$x][8] = $row['task_id'];

            $x++;
    }

    return $ticket;
}

非常感谢任何帮助!

琼斯

【问题讨论】:

标签: php datetime strtotime datetime-format


【解决方案1】:

我不确定您的日期格式是否正确。您可以通过贴出的网址在底部找到所有格式规则

date("DD/MM/YY ...

这是行不通的,因为这不是它们在 PHP 中的格式。这个:

date("d/m/y ...

将格式更改为 DD/MM/YY。 :)

http://php.net/manual/en/function.date.php

【讨论】:

  • D/m/y 会将日期打印为文本吗?
  • 如果您需要获取日期字符串的unix时间戳,strtotime($str) 函数非常棒。 php.net/manual/en/function.strtotime.php
  • 哦该死的,我的意思是小d :)
【解决方案2】:

日期("DD/MM/YY");将输出 SATSAT/DECDEC/2010201020102010..
很确定你想要date("d/m/Y",...

【讨论】:

  • 如果我把它变成这种格式并选择 13/01/2010 和 13/11/2010 当我回显转换后的日期时,它们显示为 01/01/1970 和 01/01/ 1970
  • 那么你的时间戳是错误的——即strtotime没有正确解析$form和$to。
猜你喜欢
  • 1970-01-01
  • 2012-02-24
  • 2013-09-29
  • 2023-04-08
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 2012-05-07
  • 1970-01-01
相关资源
最近更新 更多