Jump to content

Using date comparison in an if statement


ohboyatoytruck

Recommended Posts

I want to add an earlybird price to our membership system which means prices will change after a certain date.

 

Renewal date is the date that the member is renewing.

 

$renewaldate = date ("d/m/Y");

 

Fees depend on organisation size, membership year and a comparison of the current date with the earlybird expiry date which is 15/2/16.

 

if ($organisationsize == "Up to $3M" && $membershipyear = '2016' && $renewaldate >= "15/02/2016") {

$membershipfees = "242"; }
 
else if ($organisationsize == "Up to $3M" && $membershipyear = '2016' && $renewaldate < "15/02/2016") {
$membershipfees = "220"; }
 
etc.
 
But Im not getting the earlybird price ($220) being echoed with the above. 
 
All advice gratefully received.
Link to comment
Share on other sites

Dates aside, you have an error in the condition

 

$membershipyear = '2016'

 

That should use a double equal sign for comparison.

 

Also, based on what you provided I assume you have a lot of if/elseif statements for all the different permutations: different org size, renewal year, etc. You should not embed costs into code as it makes updates a complicated and potentially error prone process. It would be better to store the rates in the database and have a single function to calculate the renewal cost.

 

For example, you could have a table with the fields: size, renewal year, normal rate, early rate.

Link to comment
Share on other sites

Two options:

 

a) Compare using timestamps. If you're only doing this in PHP then sometimes that's easier, and the fact that it's just straight numbers makes it easier to understand.

$renewaldate = time();
if ($organisationsize == "Up to $3M" && $membershipyear = '2016' && $renewaldate >= mktime(0, 0, 0, 2, 15, 2016)) {
b) Compare using date strings. There are three conditions you must satisfy for this to work:

1. Reading left to right, each number in the string (eg, hour, month) must be a larger unit than the next. So you can do year/month/day (year>month>day) but not year/day/month (year>day

2. Every component must be a number, and each number must be padded (with zeroes) to the largest length. For example, days can max out at two digits long, therefore every day must be padded to two digits. So no 'D' (day name) or 'j' (unpadded day number).

3. Both strings must use the same format string. You can't compare YYYY/MM/DD with YYYY-MM-DD.

In practice that means you'll probably use one of two formats: YYYY-MM-DD ("Y-m-d") or YYYY-MM-DD HH:MM:SS ("Y-m-d H:i:s"), and the exact separator character doesn't matter.

Link to comment
Share on other sites

Thanks to everyone who responded. I found a solution. 

 

$earlybirdexpiry = "2016-02-15 00:00:00";

$today = date("Y-m-d H:i:s");

 

if ($organisationsize == "Up to $3M" && $membershipyear = "2016" && $today < $earlybirdexpiry) {

$membershipfees = "220"; }
 
And Ive fixed the quotes spotted by Psycho (thanks)
Link to comment
Share on other sites

 

Thanks to everyone who responded. I found a solution. 

 

$earlybirdexpiry = "2016-02-15 00:00:00";

$today = date("Y-m-d H:i:s");

 

if ($organisationsize == "Up to $3M" && $membershipyear = "2016" && $today < $earlybirdexpiry) {

$membershipfees = "220"; }
 
And Ive fixed the quotes spotted by Psycho (thanks)

 

 

::)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.