Ronald
Ronald

Reputation: 557

Sorting a Table with Date String

Is there a better way to build the date into a string. I need the date as a string so I can apply sorting to my table column....Looking for more efficiency... The output looks like this ( e.g. 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>";

thanks

Upvotes: 0

Views: 139

Answers (2)

user297904
user297904

Reputation: 437

I faced this problem before, It is easily solved

  $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];

Of course there are several other methods than this, but if you don't remember all the php functions this will work perfect for you .

Upvotes: 0

Ianis
Ianis

Reputation: 1175

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

Or juste use :

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

since it give you the timestamp (an integer) and will be equally efficient with less code.

Upvotes: 2

Related Questions