Jump to content

Little Help with Date/TIme Cpmparison


froggie81

Recommended Posts

Hi ; New to this forum and home a question like this is ok. I cant quote get this right . Could some one supply a little chunk of code to get me started?

 

In mysql db I have 4 fields start_date:type date - start_time:type time - end_date:type date - end_time:type time. In phpmyadmin browse function they look like 2009-08-01 and 15:00:00. These fields work perfectly for the UI I have written with drop down select lists.

 

OK , In psuodo I need to code is : if now() is >= (start date/time) and Now() <= (end date/time) bla bla bla. I am doing this check to see that someone is loggin in bewtween the allowed window. Could anyone give me a code example?

 

Thank You!

 

Frank

Link to comment
Share on other sites

You could create one field of start_date and start_time of type DATETIME and the same for end_date, end_time afterwards you can perform a query like:

 

SELECT * FROM table WHERE username = $username AND password = $password AND now() BETWEEN start AND end

 

I'm currently looking for a function that can combine a date and time field as a datetime field.

 

Edit: This should work using the start_date, start_time, end_date, end_time:

 

SELECT *
FROM table
WHERE username = $username
  AND password = $password
  AND now()
    BETWEEN date_add(start_date, interval start_time HOUR_SECOND)
        AND date_add(end_date, interval end_time HOUR_SECOND)

Link to comment
Share on other sites

Thank you all for your input. If it would be of value to anyone else,(I always appreciate when people post solutions and not disappear after getting assistance)

 

This works beautifully:

 

// EXTRACT DATA FROM DB

$start_date=$row['start_date'];

$start_time=$row['start_time'];

$end_date=$row['end_date'];

$end_time=$row['end_time'];

 

// CREATE UNIX TIMESTAMPS

 

$startdatetime=strtotime("$start_date $start_time");

$enddatetime=strtotime("$end_date $end_time");

$accesstime = time(); /* NOW */

 

if (($accesstime >= $startdatetime) && ($accesstime <= $enddatetime))

 

Thanks, Frank

 

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.