ohboyatoytruck Posted December 15, 2015 Share Posted December 15, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/299769-using-date-comparison-in-an-if-statement/ Share on other sites More sharing options...
Barand Posted December 15, 2015 Share Posted December 15, 2015 You cannot compare dates correctly using that date format. Always use yyyy-mm-dd for date storage and comparisons. Format as required on final output. Quote Link to comment https://forums.phpfreaks.com/topic/299769-using-date-comparison-in-an-if-statement/#findComment-1528019 Share on other sites More sharing options...
Psycho Posted December 15, 2015 Share Posted December 15, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/299769-using-date-comparison-in-an-if-statement/#findComment-1528020 Share on other sites More sharing options...
requinix Posted December 15, 2015 Share Posted December 15, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/299769-using-date-comparison-in-an-if-statement/#findComment-1528021 Share on other sites More sharing options...
ohboyatoytruck Posted December 15, 2015 Author Share Posted December 15, 2015 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) Quote Link to comment https://forums.phpfreaks.com/topic/299769-using-date-comparison-in-an-if-statement/#findComment-1528022 Share on other sites More sharing options...
requinix Posted December 15, 2015 Share Posted December 15, 2015 But not the equals sign? Quote Link to comment https://forums.phpfreaks.com/topic/299769-using-date-comparison-in-an-if-statement/#findComment-1528023 Share on other sites More sharing options...
maxxd Posted December 16, 2015 Share Posted December 16, 2015 For comparisons like this, I recommend using DateTime() objects. Everything suggested so far will obviously work, but I personally find them easier to deal with and more extensible as the comparison criteria inevitably grow and get more complicated. Quote Link to comment https://forums.phpfreaks.com/topic/299769-using-date-comparison-in-an-if-statement/#findComment-1528044 Share on other sites More sharing options...
Psycho Posted December 16, 2015 Share Posted December 16, 2015 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) Quote Link to comment https://forums.phpfreaks.com/topic/299769-using-date-comparison-in-an-if-statement/#findComment-1528073 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.