datetime formatting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

datetime formatting

Post 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 ?
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: datetime formatting

Post 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 ?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: datetime formatting

Post by califdon »

Where is this data coming from?
User avatar
liljester
Forum Contributor
Posts: 400
Joined: Tue May 20, 2003 4:49 pm

Re: datetime formatting

Post 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);
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: datetime formatting

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: datetime formatting

Post 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
dannyd
Forum Commoner
Posts: 56
Joined: Wed Jan 23, 2008 11:31 am

Re: datetime formatting

Post 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;
 
 
Post Reply