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

时间:2015-12-30 16:17:26

标签: javascript php sorting jquery-ui-sortable

是否有更好的方法将日期构建到字符串中。我需要将日期作为字符串,以便我可以对我的表列应用排序....寻找更高的效率...输出看起来像这样(例如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>";

感谢

2 个答案:

答案 0 :(得分:2)

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

或者juste使用:

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

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

答案 1 :(得分:0)

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

  $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功能,这对你来说是完美的。