March 10, 2012

PHP IF with multiple days of the week

Question by mrlayance

Is this possible?

$d=date("D");
...
else if ($d=='(Thu|Fri|Sat)') {

I can get a single day of the week working.

if ($d=='Wed') {

Thanks

Answer by Clamidity

You could use the OR operator:

else if ($d=='Thu' || $d=='Fri' || $d=='Sat') {

Answer by Starx

Why can’t you use or or ||?

else if ($d=='Thu' || $d=='Fri' || $d=='Sat') {

If you dont want to stick to simplicity then use preg_replace()[docs]

preg_match('^(Thu|Fri|Sat)$', $yourtext, $matches, PREG_OFFSET_CAPTURE);
if(count($matches)) {
    /// found
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!