Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

a bit more complex if statment

Status
Not open for further replies.

chriszd

Technical User
Apr 21, 2008
42
MT
i am trying to disaply the booking days of clients which will be booking online apointments:

IF the DAY, MONTH and YEAR are EQUAL or GREATER THEN todayday todaymonth and this year. the user will get the option to delete his booking,

ELSE
dispaly that booking has exipred


below is the code im using:

if ((($bookingday >= $day) && ($bookingmonth >= $monthnumber) && ($bookingyear >= $year)))
{
DISAPLY: cancel booking button
}

else
{
DISAPLY: Booking expired
}

All is wokring fine apart that if the bookingyear is for example 2010, and the month and day are SMALLER THEN the actual todayday and todaymonth

i get the
BOOKING EXPIRED message, when infact its not. since the year is bigger then the actual year.


do i have an error in the if statment, which is ignoring to check the YEAR :( ...

thanks

 
Hi

That looks horrible, at least to me. I would use [tt]mktime()[/tt] :
Code:
[b]if[/b] (mktime(0,0,0,$bookingmonth,$bookingday,$bookingyear)>=time()) {
  [gray]// booking date now or in future[/gray]
} [b]else[/b] {
  [gray]// booking date in the past[/gray]
}

Feherke.
 
looks good feherke. but does your code do what the user wanted? the test was (we are told) if the booking day is today or after today then delete otherwise no option.

your code constructs mktime as of midnight on the booking day and then tests whether that is greater than the value of time().

so if the test were run at 10h00 the mktime would not be >= time().

the easiest way to fix this using your code would be to set the time to just before midnight
Code:
if (mktime([red]23,59,59,[/red]$bookingmonth,$bookingday,$bookingyear)>=time()) {

alternatively you could use the amazing strtotime function like this

Code:
if (strtotime("$bookingyear-$bookingmonth-$bookingday") >= strtotime("today")){
 //today or in the future
} else {
 //in the past
}

neither of our code checks whether the constructed date is valid, which the OP should also test prior to testing the conditional.

note also that there is inconsistency between the today value in strtotime between php 4 and php 5. My code works for php5. Additionally strotime does not handle dates beyond 2038. If any of these are troubling then definitely go with the mktime method.
 
Hi

jpadie said:
looks good feherke. but does your code do what the user wanted?
I am not sure. I did not fully understood the requirement.

I am aware of the default midnight vs given time difference, but I ignored it. (1) The OP called them "apointments", which in my understanding should have also a time part. In that case the OP should just pass the time part too to [tt]mktime()[/tt]. (2) The OP does not mentioned from where the $booking... variables get their values. So maybe neither [tt]mktime()[/tt] or [tt]strtotime()[/tt] is necessary.

So I just cut off the theories.

Feherke.
 
hi guys...

thanks fr your help it worked fine with your suggestion Feherke.... i still got more to learn from you guys :)

infact i came accross the problem and fixed it as you suggested, but what if i can enhance the system by:

When the user books online, he also chooses the time... which are stored in the database as $bookinghour and $bookingmin

do i use this line of code?

if(mktime($bookinghour,$bookingmin,0,$bookingmonth,$bookingday,$bookingyear)>=time())

coz its not actually working for me. :)

thanks
 
personally i would store the unix timestamp in the database.

note that bookinghour needs to be in 24 hr format.
 
I agree with using unix_timestamp() and have been switching to that method. One benefit is that it uses UTC, so it's timezone-agnostic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top