0

Possible Duplicate:
In PHP given a month string such as “November” how can I return 11 without using a 12 part switch statement?

I have weekdays in a specific format that I need to convert to another format and ultimately translate them, as well. I need to do this at runtime, meaning I cannot change the format in which the dates are stored, but have to convert them for the final output on the website.

Now, of course I could just create seven if-statements to do this, like

if ($wkday == "Mon") { $wkday = "Monday"; }
if ($wkday == "Tue") { $wkday = "Tuesday"; }

...

But I'm trying to find a neater way to do this, like putting both input and output values in arrays and comparing them to one another, to cut down the number of ugly if-statements. It's not so bad in this case, seeing that there are only seven possible values, but I'd like to do it right and learn for the future. :-) I tried to search for this, but don't even know what terminology to use. I'll be grateful for any hint.

Thanks for you help!

Community
  • 1
  • 1
Jules
  • 25
  • 4
  • We were just discussing something similar, check the code example at the end of http://stackoverflow.com/a/12768452/367456 , that is a common way (array as map for translation). – hakre Oct 07 '12 at 12:27
  • Thank you, lots of good information in there! – Jules Oct 07 '12 at 13:14

2 Answers2

0

An array would be useful here, and in this case it would be similar to a hashmap in Java.

$wkday = "Thu";
$wkdayMapper = array(
  "Mon" => "Monday",
  "Tue" => "Tuesday",
  "Wed" => "Wednesday",
  "Thu" => "Thursday",
  "Fri" => "Friday",
  "Sat" => "Saturday",
  "Sun" => "Sunday"
);

echo $wkdayMapper[$wkday]; //Thursday

PHP manual link on arrays

thescientist
  • 2,906
  • 1
  • 20
  • 15
  • Jeez, of course... thank you, this is what I was looking for. Now I'm embarassed. I need to go back to PHP pre-school, it seems. :) – Jules Oct 07 '12 at 13:18
0

Use date with strtotime:

$wkday = 'Thu';
echo date('l', strtotime($wkday)); // outputs 'Thursday'
billyonecan
  • 20,090
  • 8
  • 42
  • 64
  • "very good" with different locales. and not the general approach. – hakre Oct 07 '12 at 12:34
  • plus the OP said their example was just a simplified example, and might not necessarily on be applicable to dates, although for dates this is a neat approach. :) – thescientist Oct 07 '12 at 12:35
  • I've been looking at strtotime and strftime, as well, and it would work for the moment but I'm not so sure about future requests from the client. :-) Thank you very much! – Jules Oct 07 '12 at 13:13