Kristen L
Kristen L

Reputation: 65

Convert filename-date to datetime

I have files with filenames like filename-0001#18-02-2012^16.04.43.JPG. I have split the file names to extract the date/time part. Now I want convert that part (e.g. 18/02/2012 - 16:04:43) to something like 1482581580 so I can sort by date/time.

This is my code:

$convert =

$string = $withoutExt;
$find = array('/_/', '/\./', '/-/');
$replace = array('/', ':', '/');
$result = preg_replace($find, $replace, $string);
$split   = preg_split('/\/00|\^|#/', $result);
//print_r($split);
$korrasNimi = $split[0];

$date = $split[2];
$time = $split[3];

<td data-order="<?php echo $convert ?>">
    <span class="text-center">
       <?php echo $kuupaev; echo ' - ' echo $kellaaeg; ?>
    </span>
</td>

How can I get the numerical representation of the date?

Upvotes: 0

Views: 460

Answers (1)

u_mulder
u_mulder

Reputation: 54831

If you have a string like 18/02/2012 - 16:04:43 then you can create datetime object from a certain format:

$date = DateTime::createFromFormat('d/m/Y - H:i:s', '18/02/2012 - 16:04:43');
$timestamp = $date->format('U');
echo $timestamp;

Upvotes: 1

Related Questions