Forum Moderators: coopster

Message Too Old, No Replies

preg_replace help please

need to replace something and cant get it to work

         

floz

4:10 pm on Feb 27, 2005 (gmt 0)

10+ Year Member



hi there. i never used preg_replace before and am now trying to modify a string. i tryed for some hours now, but i cant get it to work.

i'm looking at a date information (which comes as a string!) and i would like to modify it to look nice in "german" format.

the original string looks like this:

2/27/05 3:00 PM Local Time

and i would like that one to be replaced by

27.2.05, 15:00 Uhr

so a little preg_replacing needs to be done i guess and something smart to convert the am/pm-time-format to 24h format................

THX FOR YOUR HELP!

ironik

9:29 pm on Feb 27, 2005 (gmt 0)

10+ Year Member



This should replace the date format mm/dd/yy into dd.mm.yy but I'm not sure about the AM/PM thing... I'm not even sure if a regex will do it. You might need to get a regex that pulls the time match out, then use strpos() or strstr() to match the AM, PM substring and then add 12 to the hours if the time is in the PM (then you'd have to reconstruct the string). Hope that makes sense.

$date = '2/27/05 3:00 PM Local Time ';
$pattern = "/(\d{1,2})\/(\d{1,2})\/(\d{2})\b/";
$replace = "$2.$1.$3";
$newDate = preg_replace($pattern, $replace, $date);

coopster

12:43 am on Feb 28, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, floz.

The date you have there is a valid GNU Date Input Format [gnu.org] for the strtotime() [php.net] function, but you'll have to strip that 'Local Time' part off first.

$time = '2/27/05 3:00 PM Local Time'; 
$time = str_replace [php.net](' Local Time', '', $time);
$timestamp = strtotime [php.net]($time);
$formattedtime = strftime [php.net]('%d.%m.%y %H:%M Uhr', $timestamp);
$formattedtime = preg_replace [php.net]("/\.0(\d)\./", ".$1.", $formattedtime);
print "\$formattedtime: $formattedtime";

floz

8:20 am on Feb 28, 2005 (gmt 0)

10+ Year Member



thx for the welcome and

thx a lot for your help, the last one works gr8t!

this site seems very nice, i'll sure drop by more often : )