Divide a date into two parts

Asked

Viewed 447 times

3

I’m making a calendar using Mysql, html, JS and PHP.

This calendar serves to book the ballroom here in the building.

There may be two reservations on the same day, morning and afternoon that would be "00:00 until 11:59" and "12:00 until 23:59".

My question:

I would like a suggestion for the best way to do this, I have already thought to do with two fields date and the other hour.

I also wondered if it’s possible to use the datatime, timestamp.

Any hints or suggestions?

3 answers

4


I would store with 2 fields.

  • data, field date same (or int counting number of days of a standard date).

  • periodo field int 1 byte only, or enum being:

    • 1 = morning

    • 2 = late

    • 3 = both periods

Thus simplifies the problem.


Checking if the date is free.

To test date availability, just do this select:

SELECT periodo WHERE data=$data AND ( periodo=$periodo OR periodo=3 OR $periodo=3 );
  • periodo=$periodo means conflict, if you asked for it tomorrow and it’s morning, or if you asked for it late and it’s late

  • periodo=3 means conflict, because if you already have one occupying the whole day, you cannot schedule anything else on that date.

  • $periodo=3 means conflict because if you are trying to schedule all day, any existing scheduling prevents scheduling.

If the query above do not return any record, you can make a new schedule on the date $data for the period $periodo.

If, on the other hand, records return, it is not possible to put the new schedule, because it will have a conflict.

  • Not the structure of that part ( periodo=$periodo OR periodo=3 OR $periodo=3 );

  • They are the 3 tests that I explained above. period=$period eliminates late with late, and morning with morning. period=3 eliminates any scheduling if the whole day is busy. $period=3 eliminates scheduling for the whole day if anything else already exists that day.

2

The best way would be to use datatime

To pick up the dates and times separately use:

$datetime = '2010-07-05 12:11:00';

list( $date, $time ) = explode( ' ', $datetime );

echo 'Data: '.$date;

echo 'Hora: '.$time;

1

Make a boolean field (in Mysql: bit(1)) where 0 indicates the morning booking and 1 indicates the afternoon booking.

With regard to date: If it is for ease, use the DATE type, it stores dates in the following format: 'YYYY-MM-DD'

If it’s space-saving, create a SMALLINT field and store the number of days between the event and a date you set, for example between 01/01/2015 and the day of the event and let javascript do the math to turn it into a beautiful date.

You can also store everything in type DATETIME, but I find it unnecessary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.