使用日期字符串对 Table 进行排序

Sorting a Table with Date String

有没有更好的方法将日期构建到字符串中。我需要日期作为字符串,这样我就可以对我的 table 列进行排序....寻求更高的效率...输出看起来像这样(例如 20151104)

while ($row = oci_fetch_array($sql, OCI_ASSOC+OCI_RETURN_NULLS)) {

// date into string
$year = date('Y', strtotime($row['DOE']));
$month = date('m', strtotime($row['DOE']));
$day = date('d', strtotime($row['DOE']));

$strdat=($year . $month . $day);


  echo "<tr>";
  echo "<td class='ws' data-sort-value='" . $strdat . "'>" . $row['DOE'] . " </td>";

谢谢

$date = date('Ymd', strtotime($row['DOE']));

或者只使用:

$date = strtotime($row['DOE']); 

因为它给你时间戳(一个整数)并且用更少的代码同样有效。

我以前遇到过这个问题,很容易解决

  $time = time();  //You can replace it with your timestamp
  $strtime = gmdate("Y-m-d", $time);

  $arr = explode('-',$strtime);

  $days = $arr[2];
  $months = $arr[1];
  $years = $arr[0];

当然还有其他几种方法,但如果您不记得所有 php 函数,这将非常适合您。