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

Sorting a Table with Date String

本文关键字:排序 日期 字符串      更新时间:2023-09-26

是否有更好的方法将日期构建为字符串。我需要将日期作为字符串,这样我就可以将排序应用于我的表列。。。。寻求更高的效率。。。输出如下(例如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函数,这将非常适合你。