Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date formatting

Status
Not open for further replies.

bopritchard

Programmer
Joined
Jan 27, 2003
Messages
199
Location
US
how can i take this file name strip out the first 3 and last 4 characters and format the remainder as a valid date/time

cam20030903150719820.jpg
 
What are the extra three digits to be used for? Your example could be interpreted as "2003-09-03 15:07:19", but what do we do with the "820"? To the best of my knowledge, it's not part of a valid time/date stamp. I assume it's some kind of serial number or possible decimal seconds.

Anyway, this little code snippet:
Code:
<?php
$file = 'foo20030903150719820.bar';

$file = preg_replace ('/(^\D*)(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d*)/', '\2-\3-\4 \5:\6:\7.\8', $file);
list ($date, $serial) = explode('.', $file);

print $date.'<br>'.$serial;
?>

will strip out any prefixed or suffixed non-digit characters, add the dashes and colons as appropriate, and split the string into a time/date stamp and a serial number.


Want the best answers? Ask the best questions: TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top