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
https://forums.phpfreaks.com/topic/172825-little-help-with-datetime-cpmparison/
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)

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

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.