/    Sign up×
Community /Pin to ProfileBookmark

Hi all,

I would like to set a variable based on a date range. I would like to use a something like a switch for neatness. I was thinking something like:

[code=php]switch (strtotime($datetime)) {
case < strtotime(‘1 December 2008’):
$vat = ‘17.5%’;
break;

case >= strtotime(‘1 December 2008′) && <= strtotime(’31 December 2009’):
$vat = ‘15%’;
break;

case > strtotime(’31 December 2009′):
$vat = ‘17.5%’;
break;
}[/code]

What would be the neatest way to accomplish this?

Thanks!

dai.hop

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@SodbusterSep 24.2009 — I'd say that an if/else block is better suited for this:[code=php]$time = strtotime($datetime);
if (($time < strtotime('1 December 2008') || ($time > strtotime('31 December 2009')) {
$vat = '17.5%';
} else {
$vat = '15%';
}[/code]
Copy linkTweet thisAlerts:
@NogDogSep 24.2009 — For the supplied example, I would just do:
[code=php]
$vat = '17.5%';
$timestamp = strtotime($datetime);
if($timestamp >= strtotime('2008-12-01 00:00:00') && $timestamp <= strtotime('2009-12-31 23:59:59')
{
$vat = '15%';
}
[/code]

If the actual situation is more complex than that and you really want to use a switch, then it would be something like:
[code=php]
$timestamp = strtotime($datetime);
switch(true)
{
case ($timestamp < strtotime('2008-12-01 00:00:00'):
$vat = '17.5%';
break;

case ($timestamp >= strtotime('2008-12-01 00:00:00') && $timestamp <= strtotime('2009-12-31 23:59:59'):
$vat = '15%';
break;

default:
$vat = '17.5%';
}
[/code]
×

Success!

Help @dai_hop spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...