Page 1 of 1

datetime formatting

Posted: Tue Feb 19, 2008 9:58 am
by dannyd
I have a file that lists datetime such as:

060000
941070000

How would I convert a variable $datetime from 060000 to 6:00:00 and convert the second date to 07:00:00 ignoring the 941 that precedes it ?

Re: datetime formatting

Posted: Tue Feb 19, 2008 10:56 am
by dannyd
Okay,

I figured out how to convert the times into proper format 00:00:00.

How do I use regular expressions so if it starts with 941 to replace the 941 with nothing. For some reason some of the times are like 941060000 which should translate to 06:00:00.

I tried this but it doesnt work:

$time = $fields[1];

$find = "^941";
$replace = "";
$newtime = preg_replace($find,$replace,$time);

This doesnt work ... what could I be doing wrong ?

Re: datetime formatting

Posted: Tue Feb 19, 2008 11:40 am
by califdon
Where is this data coming from?

Re: datetime formatting

Posted: Tue Feb 19, 2008 11:45 am
by liljester
if the first 3 charactars are ALWAYS ignored, then just substr() the last part out.

Code: Select all

$time = substr($time, 3, 6);

Re: datetime formatting

Posted: Tue Feb 19, 2008 12:43 pm
by dannyd
califdon wrote:Where is this data coming from?
Its coming from a file heres 3 lines:

300 941223109 000020 DDD-0130
302 223130 002830 FFF-2783
307 230000 000230 EEE-3211

as you can see the second column has the time. Sometimes it has 941 which has to be ignored so the substr cant work.

Re: datetime formatting

Posted: Tue Feb 19, 2008 5:25 pm
by pickle
Regular expressions are great, but expensive in terms of clock cycles. substr() is your best bet. If it only happens conditionally, use strpos() to determine if the number starts with 941

Re: datetime formatting

Posted: Tue Feb 19, 2008 5:58 pm
by dannyd
How would I use preg_replace if I wanted ?

I tried this but didnt work:

Code: Select all

 
$fields[1] = "941060030";
 
$time = $fields[1];                     
 
$newtime = preg_replace('(PR1)','',$time);
 
echo $newtime;