0

I am pulling a filename and modified date from a directory. I am having to format the date as ymd h:i so that I can sort the array in date order using the array_multisort.

I wish to then reformat date from ymd h:i to d/m/y h:i; so 140428 11:54 would become 28/04/14 11:54.

I already have a for loop in place going through each array position basically appending it to a variable in order to print it out on the site.

<?php
$filelist = array("");
$datelist = array("");
$html = "";
$x = "";
$days = 250;
$path = 'Directory...';
if ($handle = opendir($path)) {
    while (false !== ($file = readdir ($handle))) {
        if (($file !== ".") && ($file !== "..") && ($file !== "Failed")
                && ($file !== "Complete")) {
            if (filemtime($path.'/'.$file) > (time() - ($days * 24 * 60 * 60 ) ) ) {
                array_push($filelist, $file);
                array_push($datelist, date("ymd h:i", filemtime($path.'/'.$file)));
            }
        }
    }
    closedir($handle);
    array_multisort($datelist, SORT_DESC, $filelist);
    $num = count ($filelist);
    for ($x=0; $x<$num; $x++) {
        $html .= '<tr><td><a href="file://directory/'.$filelist[$x].'">'.$filelist[$x]
            .'</a><br></td><td>'.$datelist[$x].'</td></tr>'."\n\t\t\t\t\t\t\t\t\t\t\t\t";
    }
}
?>
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
syme89
  • 3
  • 2
  • Do you mean that you know how to generate `140428 11:54` but can't figure out how to get `28/04/14 11:54`? Do you even know what line of your code is responsible for `140428 11:54`? – Álvaro González Apr 28 '14 at 11:05
  • Why are you using `date` then ? `filemtime` returns a timestamp, which is sortable without conversion. And why the hell is there an empty string when initializing the arrays ? – Sebastien C. Apr 28 '14 at 11:12
  • My apologies guys....This is the first php script I have wrote. When I wasn't initializing with an empty string the httpd error log was getting populated. Additionally from my java knowledge by populating a variable with an empty value you ensure that it overwrites it's memory allocation and does not grab an existing value. The line of code responsible for 140428 11:54 (an example) would be: array_push($datelist, date("ymd h:i", filemtime($path.'/'.$file))); As I am using array_multisort which sorts alphabetical this format seemed the simplest solution. sebcap26, how would you edit my code? – syme89 Apr 28 '14 at 12:35

2 Answers2

0

You can create a helper function to parse your custom date time string like this:

<?php

// Helper Function
function parseDate($datetime_str)
{
    list($myDate, $myTime) = explode(' ', $datetime_str);
    list($year, $month, $date) = str_split($myDate, 2);
    return "$date/$month/$year $myTime";
}

// Data Source
$datetime_str = '140428 11:54';

// Usage
var_dump(parseDate($datetime_str));

?>

Output:

enter image description here

Latheesan
  • 23,247
  • 32
  • 107
  • 201
0

I wish to then reformat date from ymd h:i to d/m/y h:i; so 140428 11:54 would become 28/04/14 11:54.

Use DateTime for such operation

$date = DateTime::createFromFormat('ymd h:i', '140428 11:54');
echo $date->format("d/m/y h:i");
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
  • I think my php version has the DateTime function unfortunately, but brilliant clean code thanks for your help :) – syme89 Apr 28 '14 at 13:07