Page 1 of 1

php change second field conditional

Posted: Wed Apr 14, 2010 9:29 am
by imadtchmn
I am checking the second field of a variable that is separated by a space to see if it has a beginning zero. If it does, replace the zero with a space. For example:
Apr 02 2010 needs to be changed to Apr 2 2010 with double spacing between the Apr and the 2.

logic is as follows

if second field first digit = '0' then change 0 to <space> else leave it alone and continue with rest of script.

Re: php change second field conditional

Posted: Wed Apr 14, 2010 9:38 am
by roders
If it's for date manipulation only then try this

Code: Select all

echo date("M j Y",strtotime("Apr 02 2010"));

Re: php change second field conditional

Posted: Wed Apr 14, 2010 12:20 pm
by imadtchmn
It is for a date field, however I am searching the directory for a specific date and time and the server uses the second space if the date is a single digit and the above does not give me that.

Re: php change second field conditional

Posted: Wed Apr 14, 2010 12:29 pm
by imadtchmn
this is the perl equivalent of what I am looking for.

Code: Select all

#!/usr/local/bin/perl -w

@date[0] = "Apr 04 20:22:16";
@date[1] = "Apr 14 21:22:16";
@date[2] = "Apr 04 22:23:16";
@date[3] = "Apr 24 23:22:16";
@date[4] = "Apr 04 24:22:16";


for (@date){
        if ($_ =~ /^(\w{3} 0)/){

                print "variable date is $_\n";

                $_ =~ s/^(\w{3} )0/$1 /g;

                print "variable date is now changed to: $_\n";
        }
        else {
                print "unchanged variable is $_\n";
        }
}